Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Reversing a string

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as mentioned above, you can use the split() method to split the string based on the empty spaces.

 
san ch
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh and here is my try at it :-)
 
Shailesh Chandra
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys

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

San
 
reply
    Bookmark Topic Watch Topic
  • New Topic