[Recursion]
Question:
A student has written the following code. It is written to check whether a string is palindrome or not. However, the code is not giving the desired result when the parameter “MADAM” is passed to the method isPalindrome(). Analyse the code and find out the logical error.
public class PalindromeTesting
{
public static boolean isPalindrome(String word)
{
if(word.length() < 1)
{
return true;
}
else if(word.charAt(0) != word.charAt(word.length() - 1))
{
return false;
}
else
{
return isPalindrome(word.substring(0, word.length() - 1));
}
}
}