Reversing a String by Word

String test = "Reverse this string";
											Stack  stack = new Stack();
											StringTokenizer strTok = new StringTokenizer(test);
											while(strTok.hasMoreTokens()) {
											stack.push(strTok.nextElement());
											}
											StringBuffer revStr = new StringBuffer();
											while(!stack.empty()) {
											revStr.append(stack.pop());
											revStr.append(" ");
											}
											System.out.println("Original string: " + test);
											System.out.println("\nReversed string: " + revStr);

The output of this code fragment will be

Original string: Reverse this string
Reversed string: string this Reverse

As you can see, reversing a string by word is more complex than reversing a string by character. This is because there is built-in support for reversing a string by character, but there is no such built-in support ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.