Write a Java program to reverse a string with an example. Reversing a string means changing the order of characters, where the last character becomes the first, the first becomes the last, and so on.
There are multiple ways to reverse a string, and in this article, we show a practical example using the following approaches:
- Using StringBuilder function.
- Using for loop
- while loop
- Using Temporary Variables to swap
- Using StringBuffer
- Recursive Functions
- Collections.reverse()
- Streams
- Stack and
- getBytes()
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());
}
}

Java Program to reverse a string 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–). Refer to Java String length article.
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 the example below, we define an empty string to store the characters. The for loop iterates over the original string from the first character to the last. The Java charAt() function adds each character to the new string. On each iteration, the incoming character is stored at the first position, and the existing characters shift right.
public class example {
public static void main(String[] args) {
String s = "Sample", ns = "";
for (int i = 0; i < s.length(); i++) {
ns = s.charAt(i) + ns;
}
System.out.println(ns);
}
}
elpmaS
Java Program to reverse a string using a character array
In this string reverse code, first, we converted the revStr to the revCharArr character array. Next, we used the Java 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 Java 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 Java 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
Program to Reverse a String Using Recursion
In this program, we defined a retxt() function that accepts a string and recursevely calls itself to reverse a string.
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
Program to reverse a string using the Collections.reverse() method
This program converts a given string to an ArrayList and uses the Collections.reverse() function to reverse it.
import java.util.ArrayList;
import java.util.Collections;
public class example {
public static void main(String[] args) {
String s = "New Year";
char[] str = s.toCharArray();
ArrayList<Character> lst = new ArrayList<>();
for (char ch : str) {
lst.add(ch);
}
Collections.reverse(lst);
System.out.println(lst);
for (char ch : lst) {
System.out.print(ch);
}
}
}
[r, a, e, Y, , w, e, N]
raeY weN
String reverse program using Stream
In modern versions, we can use streams to reverse a string. This one-line code approach is more advanced and easier to remember.
public class example {
public static String revStrwithStreams(String text)
{
return text.chars().mapToObj(ch -> (char) ch)
.reduce("", (s, c) -> c + s, (s1, s2) -> s2 + s1);
}
public static void main(String[] args) {
String s = "How are you";
System.out.println(revStrwithStreams(s));
}
}
uoy era woH
String reverse program using Stack
As the name suggests, the stack approach uses Last In, First Out (LIFO), which automatically reverses the string. Here, we define a Stack variable and push each character of the string onto it. Next, a while loop iterates over the stack; in each iteration, the pop() function retrieves the last character.
import java.util.Stack;
public class example {
public static void main(String[] args) {
String s = "New World", ns = "";
Stack<Character> stk = new Stack<>();
for (char c : s.toCharArray()) {
stk.push(c);
}
while (!stk.isEmpty()) {
ns += stk.pop(); // last character
}
System.out.println(ns);
}
}
dlroW weN
String reverse program using getBytes
First, convert the given string to a byte array and then use a for loop to iterate over it. Next, add the byte array characters in reverse order to a new one. Here, we must use “new String” to convert the byte array to a string.
public class example {
public static void main(String[] args) {
String s = "New Year";
byte[] str = s.getBytes();
int len = str.length;
byte[] ns = new byte[len];
for (int i = 0; i < len; i++) {
ns[i] = str[len - i - 1];
}
System.out.println(new String(ns));
}
}
raeY weN
Also Visit