Signup/Sign In

Java Program to Reverse a String Using Recursion

In this tutorial, we will learn how to reverse a string using a recursive function. A recursive function is a function that calls itself. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the String: String

Output: Reverse of the String is: gnirtS

Program 1: Reverse a String Using Recursion

In this program, we will see how to reverse a string using recursion with a user-defined string. Here, we will ask the user to enter the string and then we will reverse that string by calling a function recursively and finally will print the reversed string.

Algorithm

  1. Start
  2. Declare a string.
  3. Ask the user to initialize the string.
  4. Call a recursive function to reverse the string.
  5. If the string is null or consists of a single character, then print the entered string.
  6. If the string consists of multiple characters, then call the function recursively to reverse the string.
  7. Print the reversed string.
  8. Stop.

Below is the code for the same in Java language.

/*Java Program to reverse a string using Recursive Function*/
import java.util.Scanner;
public class Main
{
    //Recursive function that reverses a string
    static void reverse(String str) 
    { 
        //If the string is null or consists of single character
        //then print the entered string 
        if ((str==null)||(str.length() <= 1)) 
           System.out.println(str); 
        else
        { 
            //If string consists of multiple strings
            System.out.print(str.charAt(str.length()-1)); 
            //Call the function recursively to reverse the string
            reverse(str.substring(0,str.length()-1)); 
        }         
    }   
    // Driver Code 
    public static void main(String args[]) 
    { 
        //Take input from the user
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the String :");
        String str = sc.nextLine();   //Input the string
        //Call a recursive function to reverse the string
        System.out.println("The reverse of the entered the String :");
        reverse(str); 
    }     
}


Enter the String :
hello world
The reverse of the entered the String :
dlrow olleh

Program 2: Reverse a String Using Recursion

In this program, we will see how to reverse a string using recursion with a user-defined string. Here, we will ask the user to enter the string and then we will reverse that string by calling a function recursively and will return the reversed string. Finally, the reversed string is displayed.

Algorithm

  1. Start
  2. Declare a string.
  3. Initialize it.
  4. Call a recursive function to reverse the string.
  5. If the string is empty i.e. if the string is empty return the same string.
  6. If the string has multiple characters, then call the function recursively to reverse the string.
  7. Return the reversed string.
  8. Print the reversed string.
  9. Stop.

Below is the code for the same in Java language.

/*Java Program to reverse a string using Recursive Function*/
import java.util.Scanner;
public class Main
{
    //Driver Code
    public static void main(String[] args) 
    {
        //Take input from the user
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the String :");
        String str = sc.nextLine();   //Input the string
        //Call a recursive function to reverse the string
        String rev=reverseString(str);
        System.out.println("The reverse of the entered the String :"+rev);
 
    }
    //Recursive Function to Reverse the String
    public static String reverseString(String str)
    {
        //If entered string is empty
        //Return the empty string
        if (str.isEmpty())
            return str;
        //If string consists of multiple character    
        //Call the Function Recursively
        return reverseString(str.substring(1)) + str.charAt(0);
    }
}


Enter the String: World
The reverse of the entered the String: dlroW

Program 3: Reverse a String Using Recursion

In this program, we will see how to reverse a string using recursion with a pre-defined string.

Algorithm

  1. Start
  2. Declare a string.
  3. Initialize it.
  4. Call a recursive function to reverse the string.
  5. If the string is empty i.e. if the string is empty return the same string.
  6. If the string has multiple characters, then call the function recursively to reverse the string.
  7. Print the reversed string.
  8. Stop.

Below is the code for the same in Java language.

/*Java Program to reverse a string using Recursive Function*/
public class Main
{
    //Driver Code
    public static void main(String[] args) 
    {
        //Initialize the String
        String str = "Reverse String";
        System.out.println("The entered string is: " + str);
        String rev = reverseString(str);
        System.out.println("The reversed string is: " + rev);
    }
    //Recursive Function to Reverse the String
    public static String reverseString(String str)
    {
        //If entered string is empty
        //Return the empty string
        if (str.isEmpty())
            return str;
        //If string consists of multiple character    
        //Call the Function Recursively
        return reverseString(str.substring(1)) + str.charAt(0);
    }
}


The entered string is: Reverse String
The reversed string is: gnirtS esreveR



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.