0% found this document useful (0 votes)
3 views

RecursionFun3

The document contains a Java class named RecursionFun3 that defines a method isPalindrome, which checks if a given string is a palindrome. It handles non-letter characters by skipping them and compares characters in a case-insensitive manner. The method uses recursion to determine if the string reads the same forwards and backwards.

Uploaded by

Eeshita Soma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

RecursionFun3

The document contains a Java class named RecursionFun3 that defines a method isPalindrome, which checks if a given string is a palindrome. It handles non-letter characters by skipping them and compares characters in a case-insensitive manner. The method uses recursion to determine if the string reads the same forwards and backwards.

Uploaded by

Eeshita Soma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//Name - Eeshita Soma

//Date - 12/5/24
//Class - 4th
//Lab - Recursion Lab #3

public class RecursionFun3


{
public static boolean isPalindrome(String word)
{
if(word.length() <= 1)//base case
{
return true;
}

if(!Character.isLetter(word.charAt(0)))//skips non letters at the


beginning of the string
{
return isPalindrome(word.substring(1));//recalls recursion
}

if(!Character.isLetter(word.charAt(word.length()-1)))//skips non
letters at the end of the string
{
return isPalindrome(word.substring(0, word.length()-1));//recalls
recursion
}

word = word.toLowerCase();//makes everything lower case

if(word.charAt(0) != word.charAt(word.length()-1))//checks to see if


the corresponding letters in the beginning and end match or not
{
return false;
}

return isPalindrome(word.substring(1, word.length()-1));//recalls


recursion
}
}

You might also like