Reverse String




                           Java Program to print the Reverse String

In this blog we the java program to reverse the given string. To reverse the given string we use two variables i.e. original, reverse and reverse a string is initialized as null. Then by using a scanner, we scan the input string. This input string is stored in the original variable. Then by using length function and for loop program will reverse the string and finally stored in the reverse string. The actual code is as follows.
Code:
import java.util.*;
class ReverseString
{
public static void main(String args[])
 {
 String original,reverse="";
 Scanner in=new Scanner(System.in);
 System.out.print("Enter a string to be 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:"+reverse);
 }
}
Output:
C:\jp> javac ReverseString.java
C:\jp> java ReverseString
Enter a string to be reverse:TutorialLands
Reverse of entered string:sdnaLlairotuT
C:\jp>




Reverse String Reverse String Reviewed by Amit Waghmare on December 26, 2019 Rating: 5

No comments: