• reverse a string using recursion in java – Java Program to Reverse a String Using Recursion

• reverse a string using recursion in java: In the previous article, we have discussed about Java Program to Find Factorial of a Number Using Recursion

In this program we are going to see how to reverse a string by using Recursion in Java programming language.

Java Program to Reverse a String Using Recursion

String reverse java recursion: Let’s start with an example to understand it more clearly.

Assume a string “Hello World”
Then the reverse string = “dlroW olleH”

Now let’s see different ways to reverse a string by using Recursion.

Method-1: Java Program to Reverse a String By Using Static Input and Recursion

Approach:

  • Declare and initialize a String variable ‘str
  • Call a user defined method reverseString() method and pass the string ‘str’ as parameter.
  • Inside the user defined method we will check if the string is null or contains only 1 character then return the same string “str” to the main() method.
  • First remove the first character from the String by using charAt(0) method and append this character to end of String next follow this step by calling reverseString() method and passing str.substring(1) as parameter which means you are passing remaining character of the string.
  • Now the value of the user defined method reverseString() is stored in a String variable say ‘s’ inside the main() method.
  • Print the reverse String.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        //declare a String variable ‘str’ and initialize it
        String str = "Hello World";
        //calling reverseString() method and storing the value inside an String variable 's'
        String s = reverseString(str);
        // print the result
        System.out.println("The reverse of the String '"+str+"' is: "+s );
    }
    
    //reverseString() method to reverse the String     
    public static String reverseString(String str)  
    { 
        //if the string is null or has only 1 character then return the string 
        if (str==null || str.length()<=1)
            return str;
        //else reversing the string by calling the function recursively        
        return reverseString(str.substring(1))+str.charAt(0);
    }
}
Output:

The reverse of the String 'Hello World' is: dlroW olleH

Method-2: Java Program to Reverse a String By Using User Input and Recursion

Approach:

  • The approach is same like above approach here we are printing the last character always at start and then calling reverseString() the function recursively by passing (string.substring(0,string.length()-1) as parameter.

Program:

import java.util.*;

class Main
{ 
    public static void main(String[] args)   
    {   
        // create a scanner class
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string value: ");
        //declare an String variable ‘str’ and take the value as user input 
        String str = sc.nextLine();  
        //calling the recursive function and passing the string to be reversed   
        reverseString(str);
    }
        
        //reverseString() method to reverse the String 
        static void reverseString(String string)   
        {   
        if ((string==null)||(string.length() <= 1))   
            System.out.println(string); 
        else  
        {   
            //printing the last character first
            System.out.print(string.charAt(string.length()-1)); 
            //calling the method recursively
            reverseString(string.substring(0,string.length()-1));   
        }   
    }   
}
Output:
Enter a string value: 
I love BtechGeeks
skeeGhcetB evol I

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: