StringReverse.java

String s = "Father Charles Goes Down And Ends Battle"; // Put it in the stack frontward Stack myStack = new Stack( ); StringTokenizer st = new StringTokenizer(s); while (st.hasMoreTokens( )) myStack.push(st.nextElement( )); // Print the stack backward System.out.print('"' + s + '"' + " backwards by word is:\n\t\""); while (!myStack.empty( )) { System.out.print(myStack.pop( )); System.out.print(' '); } System.out.println('"');
Adds each one to a Stack , then processes the whole lot in LIFO order, which reverses the order.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.