13.4 Programming Activity 1: Checking for a Palindrome

In this activity, you will work with recursion to perform this function:

  • Code a recursive method to determine if a String is a palindrome.

A palindrome is a word, phrase, or sentence that is symmetrical; that is, it is spelled the same forward and backward. Examples are “otto,” “mom,” “madam,” and “able was I ere I saw elba.”

How can we determine, using recursion, whether a String is a palindrome?

If the String has two or more characters, we can check if the first and last characters are identical. If they are not identical, then the String is not a palindrome. That is a base case.

If the first and last characters are identical, then we need to check if the substring comprised of all ...

Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.