Home » Java » Core Java » Reverse A String Using Recursion

About Venkatesh Nukala

Venkatesh Nukala
Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com

Reverse A String Using Recursion

1. Introduction

In this article, You’re going to learn how to reverse a string using recursion approach. The first program is to reverse a string and the second program will read the input from the user.

In the previous articles, I have shown already how to reverse a string without using any built-in function and also how to reverse the words in a string.

2. What Is Recursion

Recursion means in computer science is that a method calling the same function with different input.

The recursive method must have at least one argument.

This approach solves many complex programs easily but you have to be very careful otherwise will create StackOverflow or outofmemoryerror.

3. Example Program to Reverse String using Recursion

To understand this program you should know two String class methods and those are charAt() and substring() methods.

package com.javaprogramto.w3schools.programs.string;

public class StringReverseRecursion {

    public static void main(String[] args) {

        String s1 = "Welcome to the javaprogramto.com";

        String reversedS1 = reverseString(s1);
        System.out.println("String s1 before reversing : "+s1);
        System.out.println("Reversed String s1 : "+reversedS1);

        String s2 = "Another String s2";

        String reversedS2 = reverseString(s2);
        System.out.println("String s2 before reversing : "+s2);
        System.out.println("Reversed String s2 : "+reversedS2);
    }


    private static String reverseString(String sentense)
    {
        if (sentense.isEmpty())
            return sentense;

        //Calling method Recursively
        return reverseString(sentense.substring(1)) + sentense.charAt(0);
    }
}

Output:

String s1 before reversing : Welcome to the javaprogramto.com
Reversed String s1 : moc.otmargorpavaj eht ot emocleW
String s2 before reversing : Another String s2
Reversed String s2 : 2s gnirtS rehtonA

4. Another Example to reverse String reading from the user

In this program, the User has to enter the string to be reversed. Scanner class nextLine() method is used to read the input string from the user keyboard and pass the string value to the recursive method reverseString().

package com.javaprogramto.w3schools.programs.string;

import java.util.Scanner;

public class StringReverseRecursionFromUser {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter String One");
        String s1 = scanner.nextLine();

        String reversedS1 = reverseString(s1);
        System.out.println("String s1 before reversing : "+s1);
        System.out.println("Reversed String s1 : "+reversedS1);

        System.out.println("Enter String Two");
        String s2 = scanner.nextLine();

        String reversedS2 = reverseString(s2);
        System.out.println("String s2 before reversing : "+s2);
        System.out.println("Reversed String s2 : "+reversedS2);
    }


    private static String reverseString(String sentense)
    {
        if (sentense.isEmpty())
            return sentense;

        //Calling method Recursively
        return reverseString(sentense.substring(1)) + sentense.charAt(0);
    }
}

Output:

Enter String One
Reading from user
String s1 before reversing : Reading from user
Reversed String s1 : resu morf gnidaeR
Enter String Two
String entered by user
String s2 before reversing : String entered by user
Reversed String s2 : resu yb deretne gnirtS

5. Conclusion

In this article, We’ve seen how to reverse a String using recursive technique.

All the code shown in this article is over GitHub.

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Reverse A String Using Recursion

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jonatan Ivanov
6 days ago

Please also mention that this method is broken and nobody should use it, here’s why:
https://dzone.com/articles/the-right-way-to-reverse-a-string-in-java

If you don’t believe me, try it out:

System.out.println(reverseString("Mathematical double-struck capital A: 𝔸"));

or

System.out.println(reverseString("Mathematical double-struck capital A: \uD835\uDD38"));

The reversed String should not start with two ‘?’ signs.

Last edited 6 days ago by Jonatan Ivanov