PDA

View Full Version : Java Help: Reversing Letters in a String


Marvel_us
02-09-07, 11:22 AM
I need to reverse letters in each string in a program that encrypts/decrypts messages in 3 ways. I've done two but can't figure out the last one. I'll just show you guys an example of what I need to do.

the quick brown fox
eht kciuq nworb xof

I can get it to reverse using the StringBuilder class method reverse() but that gives me: "xof nworb kciuq eht" which is the encryption for another class not this one.

Any suggestions? If anyone still isn't clear on what I need to do please let me know.

superklye
02-09-07, 11:58 AM
Could you perhaps break the string down into characters and put them into an array in their correct order then make a for loop the traverses the array in reverse?

Marvel_us
02-09-07, 12:11 PM
Could you perhaps break the string down into characters and put them into an array in their correct order then make a for loop the traverses the array in reverse?

That's kind of what I'm trying to do now.

Here's an example of how I did the other reverse one. In the real program the user enters the message to be encrypted but this gives you a general idea of it.

Maybe if I could do something like an if statement that executes when the current char is an empty space then reverse that, add it, and move on to the next char etc. What would be the best way to compare char to an empty space though?

StringBuilder text1 = new StringBuilder() ;

String word = "the quick brown fox" ;

for (int i = 0; i < word.length() ; i++)
{
char ch = word.charAt(i);
text1.append(ch);
}

text1 = text1.reverse();

Imbroglio
02-09-07, 12:12 PM
sounds to me like you are storing the string as one long continuous entity. what you need to do is parse the string based on ascii value 32 (space) or value 10 (new line) or value 0 (null - end of string). once you parse each string into the subsequent substrings, you can reverse the substrings and print them back adding the spaces between them.

in you for loop test to make sure the char isn't one of these values,
for (looping){
building word;
if (space/NL/LF/null){
reverse string
print string (to file or screen)
string = '\0' (clear string)
}
}//continue working through entire string.

Marvel_us
02-09-07, 12:18 PM
Thanks, question though.

For char you have to use .compareTo() right?

So it would be ch.compareTo(space)?

Imbroglio
02-09-07, 12:22 PM
not 100% sure, haven't used java in years. that's why i tried to give more of the loop outline than the code.

more than likely you wont type space, you probably compare it to 32 or to ' '

Marvel_us
02-09-07, 12:29 PM
not 100% sure, haven't used java in years. that's why i tried to give more of the loop outline than the code.

more than likely you wont type space, you probably compare it to 32 or to ' '

Yea, I didn't plan on typing space.:p

Just needed to know the proper method for comparing chars. I'll mess around with it and see what works.

Imbroglio
02-09-07, 01:08 PM
looking around found function:

public static boolean isSpace(char ch);

returns true for tab, NL, LF, CR and for ' '.

;)

Marvel_us
02-10-07, 10:39 AM
Assignment COMPLETE! Thanks for the help guys!

(nana2)

Tuork
02-10-07, 01:07 PM
Hmm... shame I saw this a bit late. I can hold my ground pretty well on Java.

What I would've done was to separare the entire string (phrase) into the individual words (an array), and then use the method you mentioned to reverse the strings.

Voila.