Write an example to find whether a given string is palindrome using recursion


Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Following is an example to find palindrome of a given number using recursive function.

Example

public class PalindromeRecursion {
   public static boolean isPalindrome(String str){

      if(str.length() == 0 ||str.length()==1){
         return true;
      }

      if(str.charAt(0) == str.charAt(str.length()-1)){
         return isPalindrome(str.substring(1, str.length()-1));
      }
      return false;
   }
   public static void main(String args[]){
      String myString = "malayalam";
      if (isPalindrome(myString)){
         System.out.println("Given String is a palindrome");
      }else{
         System.out.println("Given String is not a palindrome");
      }
   }
}

Output

Given String is a palindrome

Updated on: 13-Mar-2020

775 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements