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
- Start
- Declare a string.
- Ask the user to initialize the string.
- Call a recursive function to reverse the string.
- If the string is null or consists of a single character, then print the entered string.
- If the string consists of multiple characters, then call the function recursively to reverse the string.
- Print the reversed string.
- 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
- Start
- Declare a string.
- Initialize it.
- Call a recursive function to reverse the string.
- If the string is empty i.e. if the string is empty return the same string.
- If the string has multiple characters, then call the function recursively to reverse the string.
- Return the reversed string.
- Print the reversed string.
- 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
- Start
- Declare a string.
- Initialize it.
- Call a recursive function to reverse the string.
- If the string is empty i.e. if the string is empty return the same string.
- If the string has multiple characters, then call the function recursively to reverse the string.
- Print the reversed string.
- 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