Create a Program to Reverse a String Without Using Inbuilt Methods in Java
Reversing a string is one of the most common problems asked in beginner-level coding interviews. While Java offers built-in functions like StringBuilder.reverse() or array methods to make this task easier, understanding how to reverse a string manually without using any inbuilt methods is important to strengthen your logic-building skills.
In this tutorial, we will write a Java program that reverses a string without using any built-in methods, and we’ll explain each step in simple terms.
๐ Problem Statement
Write a Java program to reverse a string manually (without using StringBuilder, StringBuffer, Collections, or any other inbuilt reverse functions).
๐ก Approach
To reverse a string manually:
-
Read the string input from the user.
-
Convert the string into a character array.
-
Use a loop to traverse the array from the end to the beginning.
-
Construct the reversed string by appending characters in reverse order.
-
Display the reversed string.
โ Java Code to Reverse a String Without Inbuilt Methods
import java.util.Scanner;
public class ReverseStringManual {
public static void main(String[] args) {
// Step 1: Create Scanner object to read input
Scanner scanner = new Scanner(System.in);
// Step 2: Ask user to enter a string
System.out.print("Enter a string to reverse: ");
String original = scanner.nextLine();
// Step 3: Convert string to character array
char[] characters = original.toCharArray();
// Step 4: Initialize an empty string to store the reversed string
String reversed = "";
// Step 5: Loop through the character array in reverse order
for (int i = characters.length - 1; i >= 0; i--) {
reversed += characters[i]; // Append each character to 'reversed'
}
// Step 6: Print the reversed string
System.out.println("Reversed String: " + reversed);
// Close the scanner
scanner.close();
}
}
๐ง Step-by-Step Explanation
Let’s break it down line by line so that even beginners can understand:
๐น Step 1: Import Scanner and Create Main Class
import java.util.Scanner;
We import the Scanner class to read user input.
public class ReverseStringManual {
public static void main(String[] args) {
We define the main method – the entry point of our Java program.
๐น Step 2: Take Input from User
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string to reverse: ");
String original = scanner.nextLine();
-
We create a
Scannerobject to read input. -
We prompt the user to enter a string.
-
The input is stored in a variable named
original.
๐น Step 3: Convert String to Character Array
char[] characters = original.toCharArray();
-
The
toCharArray()method converts the string into a character array. -
This lets us access each letter one by one using indexes.
Note: This is just a conversion; we are not using any reverse logic here.
๐น Step 4: Initialize an Empty String for the Result
String reversed = "";
-
We create an empty string to hold the reversed result.
-
We will keep adding characters to this from the end of the original string.
๐น Step 5: Loop from End to Start of the Array
for (int i = characters.length - 1; i >= 0; i--) {
reversed += characters[i];
}
-
This loop starts from the last character (
length - 1) and ends at the first (i >= 0). -
On each iteration, it appends the character at position
ito thereversedstring.
๐น Step 6: Display the Result
System.out.println("Reversed String: " + reversed);
-
We print the reversed string to the console.
๐น Step 7: Close the Scanner
scanner.close();
-
his is good practice to release the resource used by
Scanner.
๐ Example Output
Input:
Enter a string to reverse: Hello Java
Output:
Reversed String: avaJ olleH
โ ๏ธ Important Notes
-
We used basic string and character operations.
-
We avoided using
StringBuilder,StringBuffer, or any library function that reverses strings directly. -
This method is simple and educational for understanding how strings work internally.
โ Time and Space Complexity
-
Time Complexity: O(n), where
nis the length of the string (we go through each character once). -
Space Complexity: O(n), for storing the reversed string.
๐ Conclusion
Reversing a string without inbuilt methods is a great way to build problem-solving skills in Java. This approach teaches how to manipulate strings and arrays manually, which is essential for coding interviews and logic-building exercises.
You can try improving this by:
-
Using a
char[]to build the result instead of a string (to improve performance). -
Handling null or empty string cases.
-
Making it work for multiple strings using functions.