Simple Java program to reverse a String





This Program is a to reverse a String in Java
charAt Method is used extract Characters from the string

The program as follows
import java.util.*;

class ReverseString
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);

      System.out.println("Enter the string to reverse");
      original = in.nextLine();

      int length = original.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);

      System.out.println("Reverse of entered string is: "+reverse);
   }
}




By using StringBuffer class Reverse string 
(contains a method reverse which can be used to reverse or invert an object of this class.)


class InvertString
{
   public static void main(String args[])
   {
      StringBuffer a = new StringBuffer("i love java");
      System.out.println(a.reverse());
   }
}
Previous Post Next Post