Send to a Friend

charpays's avatar

How can I use the reverse method of the string buffer using Java?

Asked by charpays (1points) March 3rd, 2010

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

Using Email

Separate multiple emails with commas.
We’ll only use these emails for this message.