Reverse a String in Java: 3 Simple Ways

“Reverse a String in Java” is a fundamental task that developers frequently encounter when working with strings in programming.

we will explore multiple approaches and algorithms to efficiently reverse a string in Java. By delving into the intricacies of string manipulation in Java, we aim to equip you with the necessary skills to tackle the task of “Reverse a String in Java” effectively. So, let’s delve into the world of Java programming and uncover the techniques to accomplish string reversal in this versatile language.

Multiple Ways to Reverse a String in Java

Reverse a String in Java
Image by rawpixel.com on Freepik

In the below program, we will use three ways to reverse a String in Java

1.  Looping through the String starting from the end and adding the characters to another StringBuffer. At the end, the StringBuffer will have the reversed String content.

2. Using Recursion to reverse a String in Java

3. Using StringBuffer’s reverse method

Method 1: Looping through the String starting from the end

package com.java.programs;

public class ReverseString {

    //Looping through the String starting from the end
    public static String reverseString(String input) {
        System.out.println("input String: " + input);
        StringBuffer output = new StringBuffer("");
        for (int i = input.length() - 1; i >= 0; i--) {
            output.append(input.charAt(i));
        }
        System.out.println("reversed string: " + output);
        return output.toString();
    }

    public static void main(String[] args) {
        reverseString("Test String");
        reverseString("1234567");
    }

}
input String: Test String
reversed string: gnirtS tseT
input String: 1234567
reversed string: 7654321

Method 2: Using Recursion to Reverse a String in Java

package com.java.programs;

public class ReverseString {

    //Using Recursion
    public static String reverseUsingRecursion(String input) {
        if (input.length() > 1) {
            return input.charAt(input.length() - 1) + reverseUsingRecursion(input.substring(0, input.length() - 1));
        } else {
            return input;
        }

    }

    public static void main(String[] args) {
        System.out.println("input String:" + "abcdef" + " , reversed String:" + reverseUsingRecursion("abcdef"));
    }

}
reversed string: 7654321

Method 3: Using StringBuffer’s reverse method

public class StringReversalExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        
        // Create a StringBuffer object with the given string
        StringBuffer stringBuffer = new StringBuffer(str);
        
        // Use the reverse() method to reverse the string
        stringBuffer.reverse();
        
        // Convert the reversed StringBuffer back to a string
        String reversedString = stringBuffer.toString();
        
        // Print the reversed string
        System.out.println("Original String: " + str);
        System.out.println("Reversed String: " + reversedString);
    }
}
Original String: Hello, World!
Reversed String: !dlroW ,olleH

Conclusion

In conclusion, reversing a string in Java is a fundamental task that can be accomplished using various approaches. Throughout this discussion, we explored three common methods for reversing a string: using a loop, using StringBuilder reverse API and using recursion.

Related Article

Simple Java Programs asked in Interviews

Leave a Reply

Your email address will not be published. Required fields are marked *