Lab 11: Palindrome

Due by the end of class

Palindromes are words or phrases that are the same spelled backwards and forwards. Examples include:

  • Racecar
  • Madam, I'm Adam
  • A man, a plan, a canal: Panama
  • Satan oscillate my metallic sonatas

The objective of this lab is to determine if an input phrase is a palindrome.

Specification

Create a project called Lab11. Add a Palindrome class (with a main() method).

Your main() method should prompt the user for a word or phrase. Create a Scanner object and use its nextLine() method to read the entire line at once, in case the user enters text with spaces in it. Then, you will need a loop that will determine if the word is a palindrome. Note that your loop must ignore spaces and punctuation and all non-letter characters. Also, your loop must ignore the cases of characters.

Your loop should follow these steps. First, keep two indexes, one for the first character in the string and one for the last. Then, make these checks:

  • If the start character is not an alphabetic character, skip it and do nothing else.
  • If the end character is not an alphabetic character, skip it and do nothing else.
  • If both the start and the end characters are alphabetic but do not have the same lower case versions, your word is not a palindrome.
  • If both the start and the end characters are alphabetic and have the same lower cases versions, move your start index forward and your end index backward.

If you keep moving the start index forward and the end index backward until they meet, and you never find two alphabetic characters that don't match, your phrase must be a palindrome.

Methods

There are a number of methods in the Character wrapper class that can make your job easier. The ones I recommend are:

  • char Character.toLowerCase(char c)
  • boolean Character.isLetter(char c)

The toLowerCase() method will give you the lower case version of a letter. For example, Character.toLowerCase('X') would return 'x'. Also, it works happily on lowercase letters and even non-alphabetic characters. Thus, Character.toLowerCase('y') would return 'y', and Character.toLowerCase('?') would return '?'.

The isLetter() method will return true if the input character is a letter and false otherwise. For example, Character.isLetter('M') would return true while Character.isLetter('7') would return false.

Sample Output

Here is an example of sample output for input Regal lager! Your output should match as closely as possible.

Enter a word or phrase: Regal lager!
Your phrase is a palindrome.
        

Here is an example of sample output for input Real lager. Your output should match as closely as possible.

Enter a word or phrase: Real lager.
Your phrase is not a palindrome.
        

Turn In

Turn in your code by uploading Palindrome.java from the Lab11\src folder wherever you created your project to Blackboard. Do not upload the entire project. I only want the Palindrome.java file.

All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.