Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Is there a way to store a series of characters as a string?

+8 votes
For example, if I want to find the reverse of a word, I thought I;d use the for loop with charAt() method and it would find each character going from right to left. But I am having trouble storing that in a string (if that is even possible).
asked Oct 6, 2016 in CSC 211 (F16) by Brandon Wilkerson (100 points)

2 Answers

+2 votes
One way to store the reverse in a string would be to use the += operator to keep adding each charAt() character onto a string. Ex. reverseWord+=word.charAt(i);
answered Oct 6, 2016 by Jared Haeme (285 points)
+1 vote
If you're talking about Exercise 4.16 then here's how I do it:
1. Create a String ( "words" for example ) to prompt the user to enter a word;

2. Create an integer (n for example) and set it to "1";

3. Create a FOR loop, set i = 0 and i < yourString.length / 2 ( cut the word into half ) and i++;

4. Create an IF statement to check : if the i character of your String (yourString.charAt(i)) is DIFFERENT than the character at words.length() - 1 - i ((yourString.charAt(yourString.length() - 1 - i))) ), then set the integer "n" above to zero. ( This way you can check if the FIRST letter of the String is identical to the last one, and everytime i goes up by 1, the last character will run backward 1 char from the last and check [ sorry Im an international ], then eventually you know if the characters of the String are the same in 2 different halves ) ;

5. Create "IF/ELSE IF" statement : IF n = 1 => palindrome , ELSE IF n = 0 => not palindrome ( I'm not sure if you could use just ELSE here because other than 1 there are many other integers, and I only previously set the integer n to zero ).

I hope this could help.
answered Oct 6, 2016 by Trung Le (Jayden) (206 points)
...