Reverse A String In Java – Here, we have discussed the various methods to reverse a string using java. The compiler has been added so that you can execute the programs by yourself, alongside suitable examples and sample outputs. The methods are as follows:
The following program to find reverse a string has been written in five different ways. If you do have any doubts please do let us know. If you need expert help with doing your java homework assigned in college or university – please visit this website.
- Java program to find a reverse a string – Using Static Method
- Using Array
- Using Recursion – In case if you have no idea check out the complete guide here.
- Using While Loop
- Using For Loop – Tutorial With Examples
- Using Word by Word
Reverse A String – Using Static Method
1) String reverse(String s) is the static method This method contains the logic to reverse the string.
2) Create the object for the class ReverseofaString and call the static method with the object as rev.reverse(str)) by passing the given string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class ReverseofaString { public static void main(String[] arg) { ReverseofaString rev=new ReverseofaString(); Scanner sc=new Scanner(System.in); System.out.print("Enter a string : "); String str=sc.nextLine(); System.out.println("Reverse of a String is : "+rev.reverse(str)); } static String reverse(String s) { String rev=""; for(int j=s.length();j>0;--j) { rev=rev+(s.charAt(j-1)); } return rev; } } |
Output:
1 2 |
Enter a string : String Reverse of a String is : gnirtS |
Java Code Reverse A String – Using Array
1) We are using a character array to reverse the given string.
2) Read the entered string using scanner object scan.nextLine() and store it in the variable str. We are converting the string a to character array the string class method toCharArray() and initialized to char[] ch.
3) j=length of the array, for loop iterates from i=length of the array to i>0. The loop prints the character which is at the index i-1 of the array “ch” i.e. it prints the characters from the last index of the char array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class ReverseofaString { public static void main(String[ ] arg) { String str; Scanner scan=new Scanner(System.in); System.out.print("Enter a string : "); str=scan.nextLine(); char[] ch=str.toCharArray(); System.out.println("Reverse of a String is :"); int j=ch.length; for(int i=j;i>0;i--) { System.out.print(ch[i-1]); } } } |
Output:
1 2 3 |
Enter a string : computer science Reverse of a String is : ecneics retupmoc |
Using Recursion
1) A method that calls itself is recursive. In this program reverse(String s) is recursive.
2) Create the object for the class ReverseofaString rev. Read the entered string using sc.nextLine() and store it in the string variable str. Call the reverse method as rev.reverse(str).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class ReverseofaString { String reverse(String s) { if(s.length() == 0) return " "; return s.charAt(s.length()-1) + reverse(s.substring(0,s.length()-1)); } public static void main(String[ ] arg) { ReverseofaString rev=new ReverseofaString(); Scanner sc=new Scanner(System.in); System.out.print("Enter a string : "); String str=sc.nextLine(); System.out.println("Reverse of a String :"+rev.reverse(str)); } } |
Output:
1 2 |
Enter a string : enter Reverse of a String :retne |
Using While Loop
1) Here i=length of the given string. While loop iterates until the condition i>0 is false, i.e. if the length of the string is zero then cursor terminates the loop.
2) While loop prints the character of the string which is at the index (i-1) until i>0. Then we will get the reverse of a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class Reverse { public static void main(String[ ] arg) { String str; Scanner scan=new Scanner(System.in); System.out.print("Enter a string : "); str=scan.nextLine(); System.out.println("Reverse of a String '"+str+"' is :"); int i=str.length(); while(i>0) { System.out.print(str.charAt(i-1)); i--; } } } |
Output:
1 2 3 |
Enter a string : kankesh Reverse of a String 'kanakesh' is : hsekanak |
Using For Loop
1) For loop iterates from j=length of the string to j>0.
2) It prints the character of the string which is at the index (i-1), then we will get reverse of a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Scanner; class ReverseofaString { public static void main(String[ ] arg) { String str; char ch; Scanner sc=new Scanner(System.in); System.out.print("Enter a string : "); str=sc.nextLine(); System.out.println("Reverse of a String '"+str+"' is :"); for(int j=str.length();j>0;--j) { System.out.print(str.charAt(j-1)); } } } |
Output:
1 2 3 |
Enter a string : java Reverse of a String 'java' is : avaj |
Word by Word
1) Read the string using scanner object scan.nextLine() and store it in the variable str. We are converting the string into the char array using the string class method toChatArray(), and initialized to char[] ch.
2) The 1st for loop iterates from i=0 to i< length of the array.
a) If ch[i]!=’ ‘ then adding ch[0] to the string word. If block adds the characters to the string word until we will get space i.e. word contains the 1st word.
b) If we get space then the else block to reverse the word which is read using else block.
3) 2nd for loop reverse the last word which is read by else block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.Scanner; class ReverseofaString { public static void main(String[ ] arg) { String str; String word=""; Scanner scan=new Scanner(System.in); System.out.print("Enter a string : "); str=scan.nextLine(); char[] ch=str.toCharArray(); for(int i=0;i<(ch.length);i++) { if(ch[i]!=' ') { word=word+ch[i]; } else { for(int c=word.length();c>0;c--) { System.out.print(word.charAt(c-1)); } System.out.print(" "); word=""; } } for(int c=word.length();c>0;c--) { System.out.print(word.charAt(c-1)); } } } |
Output:
1 2 |
Enter a string : reverse programme esrever emmargorp |