Skip to main content
Lesson 13 - String Class
Lesson MenuPreviousNext
  
L.A.13.1 - Palindrome page 10 of 12

"Madam, I'm Adam"
- Adam

"Men, I'm Eminem!"
- M. Salveit, 2000

"A man, a plan, a canal: Panama!"
- L. Mercer, 1914

"A man, a plan, a canoe, pasta, heros, rajahs, a coloratura, maps, snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana bag again (or a camel), a crepe, pins, Spam, a rut, a Rolo, cash, a jar, sore hats, a peon, a canal: Panama!"
- G. Jacobson, 1984

Background:

  1. A word is a palindrome if it reads the same forwards and backwards. For example, the word

    level

    is a palindrome

  2. The idea of a palindrome can be extended to phrases or sentences if we ignore details like punctuation. Here are two familiar examples:

    Madam, I'm Adam 
    A man, a plan, a canal: Panama

    We can recognize these more elaborate examples as palindromes by considering the text that is obtained by removing all spaces and punctuation marks and converting all letters to their lower-case form.

    Madam, I'm Adam                 -->  madamimadam
    A man, a plan, a canal: Panama  -->  amanaplanacanalpanama
  3. If the "word" obtained from a phrase in this manner is a palindrome, then the phrase is a palindrome


Assignment:

  1. Write a program that inputs a string from the user, and then tell the user whether or not it is a palindrome.

  2. Your program should ignore the case of the letter. In other words, an "a" and an "A" should be treated the same.

  3. A palindrome is determined by considering only alphabetic characters (a - z, A - Z) and numbers (0 - 9) as valid text.

  4. An input string consisting of a single character is not considered to be a palindrome.

  5. Your program should keep prompting the user for strings and printing whether or not the strings are palindromes until the user enters "Q" or "q" (which will signal termination of the program).


Instructions:

  1. Use these sample phrases as inputs for your run outputs:

    radar
    J
    Lewd did I live, & evil I did dwel.
    I like Java
    Straw? No, too stupid a fad, I put soot on warts.
  2. Here are sample run outputs.

    Welcome to the Palindrome Program!
    
    Enter a string: radar
    Yes, the string you entered is a palindrome.
    
    Enter a string: J
    No, the string you entered is NOT a palindrome.
    
    Enter a string: Lewd did I live, & evil I did dwel.
    Yes, the string you entered is a palindrome.
    
    Enter a string: I like Java
    No, the string you entered is NOT a palindrome.
    
    Enter a string: Straw? No, too stupid a fad, I put soot on warts.
    Yes, the string you entered is a palindrome.
    
    Enter a string: Q
  3. Turn in your source code and run outputs.


Note: The World's Longest Palindrome, created at 8:02 PM on the 20th of February (a palindromic time/date - 20:02 02/20 2002) is reported at http://www.norvig.com/palindrome.html


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.