A palindrome is any word phrase or sentence that reads the s

A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I. ere I saw Elba A man. a plan, a canal. Panama Desserts. I stressed Kayak Write a Boolean method that uses recursion to determine whether a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program.

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

public class PalindromeRec {

   public static boolean isPalindromeRec(String str){

       // Base case

       if(str == null || str.trim() == \"\")

           return true;

       return isPalindromeRecUtil(str, 0, str.length()-1);

   }

   public static boolean isPalindromeRecUtil(String str, int i, int j){

       // Base case

       if( i == j)

           return true;

       char ith = str.charAt(i);

       char jth = str.charAt(j);

       if(!Character.isLetter(ith) && !Character.isDigit(ith))

           return isPalindromeRecUtil(str, i+1, j); // skip special characters

       if(!Character.isLetter(jth) && !Character.isDigit(jth))

           return isPalindromeRecUtil(str, i, j-1);// skip special characters

      

       // if character is lette then converting in lower case

       if(Character.isLetter(ith))

           ith = Character.toLowerCase(ith);

       if(Character.isLetter(jth))

           jth = Character.toLowerCase(jth);

      

       if(ith != jth)

           return false;

       return isPalindromeRecUtil(str, i+1, j-1);

   }

   public static void main(String[] args) {

       System.out.println(isPalindromeRec(\"M a ,....dv... dam\"));

       System.out.println(isPalindromeRec(\"M a ,....i... dam\"));

       System.out.println(isPalindromeRec(\"A man, a Plan, a canal, panama\"));

   }

}

/*

Sample run:

true

false

true

*/

 A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I. ere I saw Elba A man.
 A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I. ere I saw Elba A man.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site