Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 02-09-07, 11:22 AM   #1
Marvel_us
FOOL!
 
Marvel_us's Avatar
 
Join Date: Dec 2005
Location: Miami, Florida
Posts: 3,754
Default Java Help: Reversing Letters in a String

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.

Quote:
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.
__________________
Main Rig
Intel Q6600 QUAD|| Gigabyte P35-DS3L ||XFX 7900GTX|| G.Skill 6GB PC2-6400 || OCZ Agility 60GB SSD || WD 320GB + WD Green 1TB HDD || Samsung 20" LCD || CoolMax 650W PSU ||

60gb PS3 Owner
PSNid: Marvel_us
Marvel_us is offline   Reply With Quote
Old 02-09-07, 11:58 AM   #2
superklye
 
Join Date: Jul 2004
Location: MKE
Posts: 13,629
Default Re: Java Help: Reversing Letters in a String

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?
superklye is offline   Reply With Quote
Old 02-09-07, 12:11 PM   #3
Marvel_us
FOOL!
 
Marvel_us's Avatar
 
Join Date: Dec 2005
Location: Miami, Florida
Posts: 3,754
Default Re: Java Help: Reversing Letters in a String

Quote:
Originally Posted by superklye
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?

Quote:
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();
__________________
Main Rig
Intel Q6600 QUAD|| Gigabyte P35-DS3L ||XFX 7900GTX|| G.Skill 6GB PC2-6400 || OCZ Agility 60GB SSD || WD 320GB + WD Green 1TB HDD || Samsung 20" LCD || CoolMax 650W PSU ||

60gb PS3 Owner
PSNid: Marvel_us
Marvel_us is offline   Reply With Quote
Old 02-09-07, 12:12 PM   #4
Imbroglio
xeroyphyte
 
Imbroglio's Avatar
 
Join Date: Mar 2004
Posts: 1,271
Default Re: Java Help: Reversing Letters in a String

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.
__________________

HT: 360 -- Sony60A3000 -- Onkyo SR605 --> Polk cs2, monitor 60s and monitor 40s w/ Bic F12 sub -- PS3
XBL:Mr X3r0 -- PSN:Mr_X3r0

Rig- Asus P6X58D Premium - intel i7 920 @3.6 - EVGA GTX570 - Corsair HX1000 - Prolimatech Mega Shadow - WD640 - COOLER MASTER ATCS 840 - OCZ Gold 6GB DDR3 1600*

Imbroglio is offline   Reply With Quote
Old 02-09-07, 12:18 PM   #5
Marvel_us
FOOL!
 
Marvel_us's Avatar
 
Join Date: Dec 2005
Location: Miami, Florida
Posts: 3,754
Default Re: Java Help: Reversing Letters in a String

Thanks, question though.

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

So it would be ch.compareTo(space)?
__________________
Main Rig
Intel Q6600 QUAD|| Gigabyte P35-DS3L ||XFX 7900GTX|| G.Skill 6GB PC2-6400 || OCZ Agility 60GB SSD || WD 320GB + WD Green 1TB HDD || Samsung 20" LCD || CoolMax 650W PSU ||

60gb PS3 Owner
PSNid: Marvel_us
Marvel_us is offline   Reply With Quote
Old 02-09-07, 12:22 PM   #6
Imbroglio
xeroyphyte
 
Imbroglio's Avatar
 
Join Date: Mar 2004
Posts: 1,271
Default Re: Java Help: Reversing Letters in a String

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 ' '
__________________

HT: 360 -- Sony60A3000 -- Onkyo SR605 --> Polk cs2, monitor 60s and monitor 40s w/ Bic F12 sub -- PS3
XBL:Mr X3r0 -- PSN:Mr_X3r0

Rig- Asus P6X58D Premium - intel i7 920 @3.6 - EVGA GTX570 - Corsair HX1000 - Prolimatech Mega Shadow - WD640 - COOLER MASTER ATCS 840 - OCZ Gold 6GB DDR3 1600*

Imbroglio is offline   Reply With Quote
Old 02-09-07, 12:29 PM   #7
Marvel_us
FOOL!
 
Marvel_us's Avatar
 
Join Date: Dec 2005
Location: Miami, Florida
Posts: 3,754
Default Re: Java Help: Reversing Letters in a String

Quote:
Originally Posted by Imbroglio
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.

Just needed to know the proper method for comparing chars. I'll mess around with it and see what works.
__________________
Main Rig
Intel Q6600 QUAD|| Gigabyte P35-DS3L ||XFX 7900GTX|| G.Skill 6GB PC2-6400 || OCZ Agility 60GB SSD || WD 320GB + WD Green 1TB HDD || Samsung 20" LCD || CoolMax 650W PSU ||

60gb PS3 Owner
PSNid: Marvel_us
Marvel_us is offline   Reply With Quote
Old 02-09-07, 01:08 PM   #8
Imbroglio
xeroyphyte
 
Imbroglio's Avatar
 
Join Date: Mar 2004
Posts: 1,271
Default Re: Java Help: Reversing Letters in a String

looking around found function:

public static boolean isSpace(char ch);

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

__________________

HT: 360 -- Sony60A3000 -- Onkyo SR605 --> Polk cs2, monitor 60s and monitor 40s w/ Bic F12 sub -- PS3
XBL:Mr X3r0 -- PSN:Mr_X3r0

Rig- Asus P6X58D Premium - intel i7 920 @3.6 - EVGA GTX570 - Corsair HX1000 - Prolimatech Mega Shadow - WD640 - COOLER MASTER ATCS 840 - OCZ Gold 6GB DDR3 1600*

Imbroglio is offline   Reply With Quote

Old 02-10-07, 10:39 AM   #9
Marvel_us
FOOL!
 
Marvel_us's Avatar
 
Join Date: Dec 2005
Location: Miami, Florida
Posts: 3,754
Default Re: Java Help: Reversing Letters in a String

Assignment COMPLETE! Thanks for the help guys!

__________________
Main Rig
Intel Q6600 QUAD|| Gigabyte P35-DS3L ||XFX 7900GTX|| G.Skill 6GB PC2-6400 || OCZ Agility 60GB SSD || WD 320GB + WD Green 1TB HDD || Samsung 20" LCD || CoolMax 650W PSU ||

60gb PS3 Owner
PSNid: Marvel_us
Marvel_us is offline   Reply With Quote
Old 02-10-07, 01:07 PM   #10
Tuork
Official pain in the ass
 
Tuork's Avatar
 
Join Date: May 2003
Location: 127.0.0.1
Posts: 1,197
Send a message via MSN to Tuork
Default Re: Java Help: Reversing Letters in a String

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.
__________________
My avatar: "An obscure and non sensical pattern made to get people to post what they think it is" - Vamp

Dimitri:
Dell XPS M1530
Core2Duo T5550 1.86Ghz | 3GB DDR2 | 8600M GT 256MB | 250GB Hdd | BenQ FP202W 20" LCD | MX1000 mouse | Inspire T5400 5.1
Tuork is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
Google (partially) loses suit to Oracle over use of Java API's ViN86 Mobile Devices And Smartphones 3 05-17-12 10:25 AM

All times are GMT -5. The time now is 06:14 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.