Question:
How do you reverse a string in java?
Answer:
Using the StringBuffer reverse() method.
Code:
public class ReverseString { public static void main(String[] args){ String forward = "the cat in the hat"; String reverse = new StringBuffer(forward).reverse().toString(); System.out.println("Before: " + forward); System.out.println("After: " + reverse); } }
Output:
$ java ReverseString Before: the cat in the hat After: tah eht ni tac eht