Send to a Friend
How can I use the reverse method of the string buffer using Java?
How can I use the reverse method of the string buffer using Java?
how can I have the user enter a string then using the reverse method, reverse the string.
For example, the method will display
?uoy era woH
for the input
How are you?
Repeat the operation until an empty string is entered.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stringrevchar;
import java.util.Stack;
import java.util.StringTokenizer;
/**
*
* @author n/a
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// StringRevChar.java
String sh = “FCGDAEB”;
System.out.println(sh + ” -> ” + new StringBuffer(sh).reverse( ));
String s = “How are you?”;
// Put it in the stack frontwards
Stack myStack = new Stack( );
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) myStack.push(st.nextElement( ));
// Print the stack backwards
System.out.print(’”’ + s + ’”’ + ” backwards by word is:\n\t\””);
while (!myStack.empty( )) {
System.out.print(myStack.pop( ));
System.out.print(’ ’);
}
System.out.println(’”’);
}
}
I used a stack but I need to do it using the reverse method instead , what is the correct code?
Using Fluther
or