MONT 105S, Spring 2009

Home | | Course Schedule | | Assignments | | Lecture Notes

MONT 105S Homework 8

Detecting Palindromes

Due Wednesday, February 18, at the beginning of class.

Palindromes
A Palindrome is a word or phrase that reads the same way forwards as backwards. For example, the following words are palindromes:

The following phrases are palindromes (if you remove the spaces and punctuation):

In this assignment, you will write a program to determine whether a word or phrase entered by the user is a palindrome. The program will remove spaces and convert all the characters to lower case before testing to see if the word or phrase is a palindrome. (We will assume the phrases will not use any punctuation).

a) Removing spaces and converting to lower case
You will first write the code to remove all the spaces and convert the word or phrase to all lower case. The program should remove the spaces by stepping through the string, one character at a time, and concatenating the characters to a new string if they are not spaces. Thus, the program should:

A sample output of this first half of the program should look as follows.

Sample Output:

Please enter a word or phrase: Able was I ere I saw Elba
ablewasiereisawelba

(User input is underlined.)

b) Detecting a palindrome

Here you will add to the program developed in part a, to determine whether the new string, (that now has spaces removed and is all lower case) is a palindrome.

To detect a palindrome, you should use the following strategy.

A sample output of the entire program should look as follows.

Sample output 1:

Please enter a word or phrase: Able was I ere I saw Elba
ablewasiereisawelba
You have entered a palindrome!

Sample output 2:

Please enter a word or phrase: Spring is coming
springiscoming
You have not entered a palindrome.

To Submit Your Finished Project:
1. Hand in a hard copy of the file <username>_palindrome.py. Hand this to your instructor in class on the project's due date.

3. Hand in a printout of 2 sample runs like those shown above. Hand this to your instructor in class on the project's due date.

4. Print your name at the top of the cover page and staple it to the top of your hard copy.

5. In addition to the hard copy listed above, email your <username>_palindrome.py file to me at croyden@mathcs.holycross.edu

Get started early and have fun!