logo Practice-It logo

isPalindrome

Language/Type: Java recursion recursive programming
Author: Stuart Reges (on 2014/02/13)

Write a recursive method called isPalindrome that takes a string as a parameter and that returns true if the string is a palindrome and false if it is not. A palindrome is a string like "racecar" that has the same sequence of characters when written forwards and backwards. Notice that in a palindrome, the first and last characters match, as do the second and second-to-last, the third and third-to-last, and so on. The table below includes various method calls and the value returned:

        Method Call       Result      Method Call               Result
        ----------------------------------------------------------------------
        isPalindrome("radar")     true        isPalindrome("123321")    true
        isPalindrome("hah")       true        isPalindrome("peer")      false
        isPalindrome("peep")      true        isPalindrome("ab")        false
        isPalindrome("x")         true        isPalindrome("a ba")      false
        isPalindrome("")          true        isPalindrome("ABba")      false

You are restricted to the following String methods:

    charAt(index)                  returns the character at the given index
    length()                       returns the length of the string
    substring(fromIndex, toIndex)  returns a new string that is a substring of this string from startIndex (inclusive) to stopIndex (exclusive)

For example, if a String s stores the text "hello", then:

    s.charAt(0) returns 'h'
    s.length() returns 5
    s.substring(1, 3) returns "el"

You are not allowed to construct any structured objects (no array, ArrayList, String, StringBuilder, etc) and you may not use a while loop, for loop, or do/while loop to solve this problem; you must use recursion.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.