Introduction
Reversing a string is one of the most common programming exercises in Java. A reverse string is created by arranging the characters of a string in the opposite order. For example, the reverse of “Java” is “avaJ”.
The Java Reverse String program is frequently asked in coding interviews because it helps beginners understand string manipulation, loops, arrays, recursion, and Java’s built-in classes.
What is a Reverse String?
A reverse string is a string whose characters appear in the opposite order of the original string.
Example:
Original String : Java
Reversed String : avaJ
More examples:
| Original String | Reversed String |
|---|---|
| Hello | olleH |
| Welcome | emocleW |
| Programming | gnimmargorP |
| level | level |
A palindrome string remains the same after reversing, while a normal string changes.
Java Program to Reverse a String Using a for Loop
public class ReverseString {
public static void main(String[] args) {
String text = "Java Programming";
String reversed = "";
for (int i = text.length() - 1; i >= 0; i--) {
reversed += text.charAt(i);
}
System.out.println("Original String: " + text);
System.out.println("Reversed String: " + reversed);
}
}
Output
Reversed String: gnimmargorP avaJ
Explanation
Step 1: Store the String
String text = "Java Programming";
This is the string we want to reverse.
Step 2: Create an Empty String
String reversed = "";
The reversed characters will be stored in this variable.
Step 3: Traverse the String Backwards
for(int i = text.length() - 1; i >= 0; i--)
The loop starts from the last character and moves toward the first character.
Step 4: Append Characters
reversed += text.charAt(i);
Each character is appended to the new string.
Step 5: Display the Result
System.out.println(reversed);
The reversed string is printed on the console.
Java Program Using StringBuilder
The easiest and most efficient approach is to use Java’s built-in StringBuilder class.
public class ReverseString {
public static void main(String[] args) {
String text = "Java Programming";
String reversed = new StringBuilder(text).reverse().toString();
System.out.println("Original String: " + text);
System.out.println("Reversed String: " + reversed);
}
}
Output
Reversed String: gnimmargorP avaJ
Advantages
- Less code
- Easy to understand
- Better performance
- Recommended for large strings
Java Program Using Character Array
public class ReverseString {
public static void main(String[] args) {
String text = "Java";
char[] characters = text.toCharArray();
for (int i = characters.length - 1; i >= 0; i--) {
System.out.print(characters[i]);
}
}
}
Output
Java Program Using Recursion
public class ReverseString {
static void reverse(String text) {
if (text.length() == 0)
return;
System.out.print(text.charAt(text.length() - 1));
reverse(text.substring(0, text.length() - 1));
}
public static void main(String[] args) {
reverse("Java");
}
}
Output
Java Program Using User Input
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String text = scanner.nextLine();
String reversed = "";
for (int i = text.length() - 1; i >= 0; i--) {
reversed += text.charAt(i);
}
System.out.println("Reversed String: " + reversed);
scanner.close();
}
}
Output
Reversed String: IAnepO
Algorithm
- Read the string.
- Create an empty string.
- Traverse the string from the last character to the first.
- Append each character to the new string.
- Print the reversed string
Flowchart
Start
│
Read String
│
Create Empty String
│
Traverse String from End
│
Append Characters
│
Display Reversed String
│
End
Time Complexity
| Method | Time Complexity |
|---|---|
| for Loop | O(n) |
| Character Array | O(n) |
| StringBuilder | O(n) |
| Recursion | O(n) |
where n is the length of the string.
Space Complexity
| Method | Space Complexity |
|---|---|
| for Loop | O(n) |
| Character Array | O(n) |
| StringBuilder | O(n) |
| Recursion | O(n) |
Common Mistakes
1. Incorrect Loop Condition
Incorrect:
for(int i = 0; i < text.length(); i++)
This prints the original string.
Correct:
for(int i = text.length() - 1; i >= 0; i--)
2. Accessing an Invalid Index
Incorrect:
text.charAt(text.length());
This causes:
StringIndexOutOfBoundsException
Correct:
text.charAt(text.length() - 1);
3. Comparing Strings with ==
Incorrect:
if(text == reversed)
Correct:
if(text.equals(reversed))
4. Forgetting to Close Scanner
Always close the scanner after use.
scanner.close();
5. Using String Concatenation for Very Large Strings
Repeated string concatenation creates many temporary objects.
Use:
String reversed = new StringBuilder(text).reverse().toString();
for better performance.
Real-Life Applications
Reversing strings is useful in many real-world scenarios, including:
- Palindrome checking
- Text processing
- Data transformation
- Encryption and decryption
- Compiler design
- Coding interview questions
- Competitive programming
- Search algorithms
Summary
The Java Reverse String program is one of the most important beginner-level programming exercises. It teaches essential concepts such as string manipulation, loops, recursion, arrays, and Java's built-in classes.
While there are multiple ways to reverse a string, the StringBuilder.reverse() method is the simplest and most efficient for most applications. Understanding string reversal also lays the foundation for solving related problems such as Palindrome Check, Reverse Words in a Sentence, String Comparison, and Anagram Detection.