Signup/Sign In

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

  1. Start
  2. Declare a string variable.
  3. Ask the user to initialize the string.
  4. Call a function to check whether the string is palindrome or not.
  5. If a string is empty, then it is a palindrome.
  6. If the string is not empty, then call a recursive function.
  7. If there is only one character, then it is a palindrome.
  8. If the first and last characters do not match, then it is not a palindrome.
  9. If there are multiple characters, check if the middle substring is also palindrome or not using the recursive function.
  10. Print the result.
  11. 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

  1. Start
  2. Declare a string variable.
  3. Ask the user to initialize the string.
  4. Call a recursive function to check whether the string is palindrome or not.
  5. If a string is empty or if it consists of only one character, then it is a palindrome.
  6. If there are multiple characters, then the first and last character of the string is checked.
  7. 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.
  8. Continue the process until the condition fails.
  9. Display the result.
  10. 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



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.