//TestPalindrome.java
//Determines if a string is a palindrome.

public class TestPalindrome
{
    public static void main(String[] args)
    {
        System.out.println
        (
            "\nThis program tests some strings and "
            + "reports whether they are palindromes.\n"
        );
        System.out.println
        (
            "\"noon\""
            + (isPalindrome("noon", 0, "noon".length() - 1)
               ? " is " : " is not ") + "a palindrome."
        );
        System.out.println
        (
            "\"eve\""
            + (isPalindrome("eve", 0, "eve".length() - 1)
               ? " is " : " is not ") + "a palindrome."
        );
        System.out.println
        (
            "\"morn\""
            + (isPalindrome("morn", 0, "morn".length() - 1)
               ? " is " : " is not ") + "a palindrome."
        );
        String testStr = "ablewasiereisawelba";
        System.out.println
        (
            testStr
            + (isPalindrome(testStr, 0, testStr.length() - 1)
               ? " is " : " is not ") + "a palindrome."
        );
        testStr = "amanaplanacanalpanama";
        System.out.println
        (
            testStr
            + (isPalindrome(testStr, 0, testStr.length() - 1)
               ? " is " : " is not ") + "a palindrome."
        );
        testStr = "amanaplanacanapanama";
        System.out.println
        (
            testStr
            + (isPalindrome(testStr, 0, testStr.length() - 1)
               ? " is " : " is not ") + "a palindrome."
        );
    }

    public static boolean isPalindrome
    (
        String s,
        int first,
        int last
    )
    /** Determine whether a string is a palindrome.
        @return true if s is a palindrome between the indices first
        and last (inclusive), false otherwise.
        @param s The full string to be tested.
        @param first The lower index of the portion of the string
        to be tested on the current call.
        @param The upper index of the portion of the string to be
        tested on the current call.
        <p>Pre:<p>s contains a non-empty string object and
        0 <= first, last <= s.length().
        Post:<p>No side effects.
    */
    {
        if (first >= last)
            return true;
        else
            return (s.charAt(first) == s.charAt(last) &&
                    isPalindrome(s, first + 1, last - 1));
    }
}

