Reverse a String in Java Without Using Built-in Methods: Step-by-Step Guide

By Vandu
May 2, 2025

Follow us on


Learn how to reverse a string in Java without using inbuilt methods. A beginner-friendly tutorial with simple explanations and clean code examples.

Reverse a String in Java Without Using Built-in Methods: Step-by-Step Guide

Create a Program to Reverse a String Without Using Inbuilt Methods in Java

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.


🔹 Why Learn This?

  • Helps improve your understanding of strings.

  • Teaches you how loops and character arrays work.

  • Strengthens your logic-building skills.


🔹 Problem Statement

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().


🧾 Java Code: Reverse String Without Inbuilt Methods

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();
    }
}

🔍 Step-by-Step Explanation

✅ Step 1: Taking User Input

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();

✅ Step 2: Convert String to Character Array

We use toCharArray() to break the string into characters. This helps us access each character using its index.

char[] chars = input.toCharArray();

✅ Step 3: Initialize an Empty String

We need a new string where we will store the reversed characters.

String reversed = "";

✅ Step 4: Loop in Reverse

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.

✅ Step 5: Print the Result

After the loop ends, we simply print the reversed string.

System.out.println("Reversed string: " + reversed);

✅ Step 6: Close the Scanner

Good practice is to close the scanner object to prevent resource leaks.


🎯 Output Example

Input:

Enter a string to reverse: Hello Java

Output:

Reversed string: avaJ olleH

✅ Bonus Tip: Improve Efficiency (Optional)

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).


📌 Conclusion

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.).


© 2025 Pay18News. All rights reserved.