Learn how to reverse a string in Java without using inbuilt methods. A beginner-friendly tutorial with simple explanations and clean code examples.
Reversing a string is one of the most common beginner-level programming problems. But what if you're asked to do it without using built-in Java methods like reverse()
or StringBuilder
? Don’t worry—this guide will walk you through a step-by-step approach to solve this in pure Java.
Helps improve your understanding of strings.
Teaches you how loops and character arrays work.
Strengthens your logic-building skills.
Write a Java program that takes a string from the user and reverses it without using any built-in method like StringBuilder.reverse()
or StringBuffer.reverse()
.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// Step 1: Create Scanner object to take input from user
Scanner scanner = new Scanner(System.in);
// Step 2: Ask the user to enter a string
System.out.print("Enter a string to reverse: ");
String input = scanner.nextLine();
// Step 3: Convert the string into a character array
char[] chars = input.toCharArray();
// Step 4: Create a new empty string to store reversed result
String reversed = "";
// Step 5: Loop through the character array from end to start
for (int i = chars.length - 1; i >= 0; i--) {
reversed = reversed + chars[i]; // Add each character to reversed
}
// Step 6: Print the reversed string
System.out.println("Reversed string: " + reversed);
// Step 7: Close the scanner
scanner.close();
}
}
We use Scanner
to take input from the user. This helps us get a dynamic string instead of hardcoding it.
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
We use toCharArray()
to break the string into characters. This helps us access each character using its index.
char[] chars = input.toCharArray();
We need a new string where we will store the reversed characters.
String reversed = "";
Now we go from the last character to the first using a for
loop.
for (int i = chars.length - 1; i >= 0; i--) {
reversed = reversed + chars[i];
}
Each character is added to the reversed
string one by one.
After the loop ends, we simply print the reversed string.
System.out.println("Reversed string: " + reversed);
Good practice is to close the scanner object to prevent resource leaks.
Input:
Enter a string to reverse: Hello Java
Output:
Reversed string: avaJ olleH
The current method uses string concatenation, which can be slow for very long strings. You could use a char[]
to store reversed characters or use StringBuilder
(if allowed in future exercises).
Reversing a string without using inbuilt methods is a great exercise to improve your Java fundamentals. This method teaches you about loops, character arrays, and how strings behave in Java.
Practice this with different input strings and try writing variations (e.g., reverse only words, ignore spaces, etc.).
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!