Java Program to check Palindrome string using Recursion
In this tutorial, we will learn how to check whether a string is a palindrome or not 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: Mom
Output: The entered string is a palindrome.
Let us look at the program to check whether the string is a palindrome or not.
Program 1: Check Palindrome String using Recursion
In this program, we will learn how to check whether a string is a palindrome or not using recursion. Here, we will ask the user to enter the string. Then, we will call a separate recursive function to check whether the string is a palindrome or not only if the entered string is non-empty. If the string is empty then it will print that it is a palindrome.
Algorithm
- Start
- Declare a string variable.
- Ask the user to initialize the string.
- Call a function to check whether the string is palindrome or not.
- If a string is empty, then it is a palindrome.
- If the string is not empty, then call a recursive function.
- If there is only one character, then it is a palindrome.
- If the first and last characters do not match, then it is not a palindrome.
- If there are multiple characters, check if the middle substring is also palindrome or not using the recursive function.
- Print the result.
- Stop.
Below is the code for the same in Java language.
/*Java Program to Check whether a String is a Palindrome or not using Recursive Function*/
import java.util.Scanner;
public class Main
{
//Recursive function that checks
//whether the string is palindrome or not
static boolean checkPalindrome(String str, int s, int e)
{
if (s == e) // If there is only one character
return true;
// If first and last characters do not match
if ((str.charAt(s)) != (str.charAt(e)))
return false;
// If there are multiple characters, check if
// middle substring is also palindrome or not.
if (s < e + 1)
return checkPalindrome(str, s + 1, e - 1);
return true;
}
static boolean isPalindrome(String str)
{
int n = str.length();
// If string is empty,then it is palindrome
if (n == 0)
return true;
return checkPalindrome(str, 0, n - 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
//Check whether palindrome or not
if (isPalindrome(str))
System.out.println(str+" is palindrome");
else
System.out.println(str+ " is not a palindrome");
}
}
Enter the String: wow
wow is palindrome
Program 2: Check Palindrome String using Recursion
In this program, we will learn how to check whether a string is a palindrome or not using recursion. Here, once the string is entered by the user we will call a recursive function to check whether it is a palindrome or not by comparing the first and last character of the substring.
Algorithm
- Start
- Declare a string variable.
- Ask the user to initialize the string.
- Call a recursive function to check whether the string is palindrome or not.
- If a string is empty or if it consists of only one character, then it is a palindrome.
- If there are multiple characters, then the first and last character of the string is checked.
- If the first and last characters of the string are the same then carry out the same for substring with the first and last character removed.
- Continue the process until the condition fails.
- Display the result.
- Stop.
Below is the code for the same in Java language.
/*Java Program to Check whether a String is a Palindrome or not using Recursive Function*/
import java.util.Scanner;
public class Main
{
//Recursive function that checks
//whether the string is palindrome or not
static boolean isPalindrome(String str)
{
//If string has 0 or 1 character
if(str.length() == 0 || str.length() == 1)
return true;
//If string has multiple characters
//Check whether first and last characters are same or not
if(str.charAt(0) == str.charAt(str.length()-1))
return isPalindrome(str.substring(1, str.length()-1));
return false;
}
// 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
//Check whether palindrome or not
if (isPalindrome(str))
System.out.println(str+" is palindrome");
else
System.out.println(str+ " is not a palindrome");
}
}
Enter the String: hello
hello is not a palindrome