Java Program to print the Reverse StringIn 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.javaC:\jp> java ReverseStringEnter a string to be reverse:TutorialLandsReverse of entered string:sdnaLlairotuTC:\jp>
Reverse String
Reviewed by Amit Waghmare
on
December 26, 2019
Rating:
No comments: