Saturday, December 5, 2015

Java pogram to reverse a string

Java pogram to reverse a string:


package javaapplication1;
import java.util.Scanner;

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

      System.out.println("Enter a string to reverse");
      orig = in.nextLine();

      int length = orig.length();

      for ( int i = length - 1 ; i >= 0 ; i-- ){
         rev = rev+ orig.charAt(i);}

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

0 comments:

Post a Comment