javacodex.com
Java Examples
Java Examples
memu home questions

How to reverse a String without using the built In reverse method or recursion?

ReverseString.java

public class ReverseString {
 
   public static void main(String[] args) {
      String str1 = "The cat in the hat.";
      char[] array1 = str1.toCharArray();
      char[] array2 = new char[array1.length];
 
      for (int i=0, j=array1.length-1; i < array1.length; i++, j--) {
         array2[j] = array1[i];
      }
      String str2 = String.valueOf(array2);
 
      System.out.println("str1: " + str1);
      System.out.println("str2: " + str2);
   }
}
 

output

$ java ReverseString
str1: The cat in the hat.
str2: .tah eht ni tac ehT

Questions answered by this page:

How to reverse a String without using the built In reverse method or recursion? Java Program To Reverse A String without String.reverse() How can I char array in reverse order? Print a char array in reverse order using recursion?




Contact: javacodex@yahoo.com