I Spy Code - Java

Reverse a string

Question:

Write a java program that reverses a string.

Solution:

Here is a java example that reverses a string:

Source: (Example.java)

import org.apache.commons.lang.StringUtils;
 
public class Example {
 
   public static void main(String[] args) {
 
      String str1 = "The cat in the hat";
      System.out.println(str1);
 
      String str2 = StringUtils.reverse(str1);
      System.out.println(str2);
 
   }
}
 

Output:

$ java Example
The cat in the hat
tah eht ni tac ehT

References:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#reverse-java.lang.String-

Questions answered by this page:

Java programming code to reverse a string