import java.util.Scanner;

/* 
 * Problem 5:  HS CS programming Contest (2006)
 * PalindromeDriver - to test if a string is a palindrome or not 
 *                      uses recursive function!
 * Author:  William Kreahling (based on a program in Big Java 2nd edition)
 * Date:    March 2006
 * NOTE:    Uses Scanner class
 */

public class PalindromeDriver
{
   public static void main(String[] args)
   {
      Scanner scanIn = new Scanner(System.in);

      // Get the String to check
      System.out.print("Enter a possible palindrome >");
      String sentence = scanIn.nextLine();

      if (isPalindrome(sentence))
         System.out.println("Yes \"" + sentence + "\" is a palindrome.");
      else
         System.out.println("No \"" + sentence + "\" is a palindrome.");
   } 

   /*
    * ispalindrome - recursive function that determines if the string 
    *                passes in is a palindrome 
    * Parameters:    text - string to check
    */
   public static boolean isPalindrome(String text)
   {
      int length = text.length();
   
      // base case - 1 letter is a palindrome
      if (length <= 1)
         return true;

      // Get first and last characters (converted to lowercase)
      char first = Character.toLowerCase(text.charAt(0));
      char last = Character.toLowerCase(text.charAt(length - 1));

      // Are both of these Letters
      if (Character.isLetter(first) && Character.isLetter(last))
      {
         if (first == last)
         {
            String shortString = text.substring(1, length - 1);
            return isPalindrome(shortString);
         }
         else
            return false;
      
      }
      // we are only worried about letters not punctuation
      // so if the current 'last' char is punction don't include it in 
      // the string we check in the next iteration
      else if (!Character.isLetter(last))
      {
       
         String shortString = text.substring(0, length - 1);
         return isPalindrome(shortString);
      }
      // we are only worried about letters not punctuation
      // so if the current 'first' char is punction don't include it in 
      // the string we check in the next iteration
      else
      {
         String shortString = text.substring(1);
         return isPalindrome(shortString);
      }
   }
}
