Topical Information

This lab will help you practice with strings.

Program Information

Write a program to determine if the user's input is a palindrome or not. Recall that a palindrome is a word which is the same when spelled forward or backward:

    deed
    radar
    mom
    Otto
    2112
    racecar

(I didn't say they had to make sense...)

The only trick to palindromes is remembering to ignore capitalization.

In another text (or was it on the InterWebs..?) we find them creating the reverse of the string and comparing this to the original. It may seem clever at first, but it isn't a good approach. Neither is the common part of the solution to make the string all lower-case before comparing. (Nor is removing all the punctuation/spacing by duplicating what isn't into a copy of the string when doing phrase-style palindromes...not that that would concern us. *shrug*)

All of these suffer from two problems: duplicate storage of essentially the same information when we aren't going to ever need it again and processing the user's string multiple times to decide if it is a palindrome or not. (Some suffer that last problem in spades!)

You'll need to remove all these inefficiencies in your solution. You should only have to make one pass through the string to determine its palindromicity (is that even a word?? *shrug*).

Ideally, your code for determining palindrom-ness would be encapsulated within a function — making the main little more than a nice driver. However, I suppose that could be an option. *sigh*

Thought Provoking Questions

  1. How can you access individual characters within a string?

  2. How can you tell how long a string's content is?

  3. How can you tell that the characters at the beginning and end of a word are the same?

  4. What do you do with odd length words?

  5. How many characters do you have to look at to determine a word's 'palindromicness' if it contains 10 characters? 11 characters? n characters?

  6. Does your program consider 34743 a palindrome? Why/Why not?

This assignment is (Level 2.5).

Options