codescracker


java

Java Program to Reverse String



« Previous Program Next Program »


Reverse String

To reverse any string in Java Programming, you have to ask to the user to enter the string and start placing the character present at the last index of the original string in the first index of the reverse string (make a variable say rev to store the reverse of the original string).

Java Programming Code to Reverse String

Following Java Program ask to the user to enter a string to reverse it and display the reversed string on the screen:

/* Java Program Example - Reverse a String */
		
import java.util.Scanner;

public class JavaProgram
{
   public static void main(String args[])
   {
      String orig, rev="";
      int i, len;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter a String to Reverse : ");
      orig = scan.nextLine();
      
      len = orig.length();
	  
      for(i=len-1; i>=0; i--)
      {
          rev = rev + orig.charAt(i);
      }
	  
        System.out.print("Reverse of Entered String is : " +rev);
   }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program to reverse string

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test