Possible Duplicate:
Reverse a given sentence in java
what is the simple way to reverse the words in string in java? Example: "Hello Stack over flow" will turn to: "flow over Stack Hello"
Thank you,
Possible Duplicate:
Reverse a given sentence in java
what is the simple way to reverse the words in string in java? Example: "Hello Stack over flow" will turn to: "flow over Stack Hello"
Thank you,
Here is a simple solution:
String s = "Hello stack over flow";
List< String > words = Arrays.asList( s.split( " " ) );
Collections.reverse( words );
String reversed = words.get( 0 );
for ( int i = 1; i < words.size(); ++i ) {
reversed += " " + words.get( i );
}
I assume that all characters except spaces are considered as part of words. You may need to provide more details if you need something else.
String s="abcd";
StringBuffer sb=new StringBuffer(s);
sb=sb.reverse();
System.out.println(sb.toString());
Hope can help you.
A straightforward solution would be splitting the input string at the whitespaces, then reversing the result array with eg. Collections.reverse() or Commons ArrayUtils.reverse(), then joining them back together.