wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Reversing a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reversing a string " Watch "Reversing a string " New topic
Author

Reversing a string

san ch
Ranch Hand

Joined: Oct 18, 2004
Posts: 68
Hi can some body help me with reversing a string
i.e if the string is "this is me" it should print it as
"me is this"

i trie dit by using stringtokenizer but messed it up terriblly


thank you
san
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Try using the split("\\s") method to split the string into an array and then do a reverse loop through its elements.

You don't have to use the StringTokenizer class.
[ February 03, 2005: Message edited by: Vicken Karaoghlanian ]

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by san ch:
i trie dit by using stringtokenizer but messed it up terriblly

What do you mean by this? Does the above program compile? If not, what errors do you get? If it does, what happens when you try to run it? It would help if you show us the output you get and explain how it differs from what you expect.

If you provide details like this, we will be more than happy to help you track down your problems and find solutions to them.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Patrick Joseph
Greenhorn

Joined: Jan 11, 2005
Posts: 11
as mentioned above, you can use the split() method to split the string based on the empty spaces.

san ch
Ranch Hand

Joined: Oct 18, 2004
Posts: 68
Hi

Sorry for not sharing the output details layne ,i didnt get how to go about with the string tokenizer .....so gave up on that .....

Thank you for your help PJ and vicken

san
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

san,

there is one more way... do the following

for example you have a string like


String str = "This is my String ";
StringBuffer sb = new StringBuffer(str);
str = ((StringBuffer)sb.reverse()).toString();


your string is now reversed


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
Shailesh :
your code does not produce the result San is looking for. output would be
gnirtS ym si sihT

instead of
String my is This

and the Cast to StringBuffer is not required. This will compile (but not produce what San is looking for :-) )



pascal
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
oh and here is my try at it :-)
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by pascal betz:

your code does not produce the result San is looking for. output would be




Pascal,

Thanks for remindig me !!!

I skipped expected resulted as thread was subjected to reversing String !!! NOT reversing sentense

anyway thanks

Shailesh
Raj Maheshwari
Ranch Hand

Joined: Jul 02, 2004
Posts: 34
Try This:
---------------------------------------
import java.util.*;

public class StringReverse {

public static void main(String args[]){

String myStr = "This is the string";
Vector v = new Vector();

System.out.println("Original String: "+myStr);

StringTokenizer st = new StringTokenizer(myStr);


while(st.hasMoreTokens()){

v.insertElementAt(st.nextToken(),0);
}

// at this point we have the words in a vector.

System.out.print("Reversed String: ");
for(int size=0;size<v.size();size++){
System.out.print(v.elementAt(size)+" ");
}


}

}
san ch
Ranch Hand

Joined: Oct 18, 2004
Posts: 68
Thank you guys

because of your valuable posts i could learn different ways of doing it

San
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reversing a string