Write a Java program to reverse a string with an example. We can do this in multiple ways:
- Using StringBuilder function.
- Using for loop
- while loop
- Using Temporary Variables to swap
- Using StringBuffer
- Recursive Functions
Java Program to Reverse a String using StringBuilder
We use the StringBuilder class to reverse the given string in this example. Here, StringBuilder(revStr) will create a StringBuilder of name sb. The sb.reverse() inside the println() statement calls the reverse() function available in the StringBuilder class to reverse the text.
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { String revStr; sc= new Scanner(System.in); System.out.print("\nPlease Enter Text = "); revStr = sc.nextLine(); StringBuilder sb = new StringBuilder(revStr); System.out.println("The result = " + sb.reverse()); } }
Using for loop
In this example, we used for loop to iterate revStr from end to start in reverse order. Next, we concat each character to the newStr. Here, the length() function finds the length, and don’t forget to decrease the i value (i–).
import java.util.Scanner; public class Example2 { private static Scanner sc; public static void main(String[] args) { String revStr; int i; sc= new Scanner(System.in); System.out.print("\nPlease Enter = "); revStr = sc.nextLine(); String newStr = ""; for(i = revStr.length() - 1; i >= 0; i--) { newStr += revStr.charAt(i); } System.out.println("The result = " + newStr); } }
Please Enter = text revrse
The result = esrver txet
In this string reverse code, first, we converted the revStr to the revCharArr character array. Next, we used the for loop to traverse revCharArr from right to left, assign the last value to the first index, etc.
import java.util.Scanner; public class Example3 { private static Scanner sc; public static void main(String[] args) { String revStr; int i, j = 0; sc= new Scanner(System.in); System.out.print("\nPlease Enter Text = "); revStr = sc.nextLine(); char[] revCharArr = revStr.toCharArray(); System.out.print("\nThe result = " ); for(i = revCharArr.length - 1; i >= 0; i--) { revCharArr[j++] = revStr.charAt(i); } System.out.print(revCharArr); } }
Please Enter Text = Hello
The result = olleH
In this code for string reverse, we used a temporary variable to hold and swap the values.
import java.util.Scanner; public class Example5 { private static Scanner sc; public static void main(String[] args) { String revStr; char temp; sc= new Scanner(System.in); System.out.print("\nPlease Enter Text = "); revStr = sc.nextLine(); char[] revCharArr = revStr.toCharArray(); int len = revStr.length() - 1; System.out.print("\nThe result = " ); for(int i = 0; i < revCharArr.length / 2; i++) { temp = revCharArr[i]; revCharArr[i] = revCharArr[len]; revCharArr[len--] = temp; } System.out.print(revCharArr); } }
Java Program to Reverse a String using While Loop
This example is the same as the above for loop example. However, we replaced the for with a while loop.
import java.util.Scanner; public class Example4 { private static Scanner sc; public static void main(String[] args) { String revStr; sc= new Scanner(System.in); System.out.print("\nPlease Enter = "); revStr = sc.nextLine(); char[] revCharArr = revStr.toCharArray(); String newSt = ""; int i = revCharArr.length - 1; System.out.print("\nThe first result = " ); while(i >= 0) { System.out.print(revCharArr[i]); newSt += revCharArr[i]; i--; } System.out.println("\nThe Second result = " + newSt); } }
Please Enter = Programming World
The first result = dlroW gnimmargorP
The Second result = dlroW gnimmargorP
Reverse a String using StringBuffer
Similar to the StringBuilder class, the StringBuffer has a reverse() function. So, we convert the given revStr to sbuff (StringBuffer) and invert the same. Please refer to the Java tutorial.
import java.util.Scanner; public class Example6 { private static Scanner sc; public static void main(String[] args) { String revStr; sc= new Scanner(System.in); System.out.print("\nPlease Enter Text = "); revStr = sc.nextLine(); StringBuffer sbuff = new StringBuffer(revStr); System.out.println("The result = " + sbuff.reverse()); } }
Please Enter Text = tutorial gateway
The result = yawetag lairotut
Java Program to Reverse a String Using Recursion
import java.util.Scanner; public class Example7 { private static Scanner sc; public static void main(String[] args) { String revStr; sc= new Scanner(System.in); System.out.print("\nPlease Enter Text = "); revStr = sc.nextLine(); sc.close(); String rev = retxt(revStr); System.out.print("\nThe result = " + rev ); } public static String retxt(String revStr) { if(revStr.isEmpty()) { return revStr; } return retxt(revStr.substring(1)) + revStr.charAt(0); } }
It is an extension of the above program.
import java.util.Scanner; public class Example8 { private static Scanner sc; public static void main(String[] args) { String revStr; sc= new Scanner(System.in); System.out.print("\nPlease Enter text = "); revStr = sc.nextLine(); System.out.print("\nThe result = " ); rvstDef(revStr); } public static void rvstDef(String revStr) { if(revStr.isEmpty()) { System.out.println(revStr); } else { System.out.print(revStr.charAt(revStr.length() - 1)); rvstDef(revStr.substring(0,revStr.length() - 1)); } } }
Please Enter text = This Tutorial
The result = lairotuT sihT