Lesson goal: Finding Palindromes

Previous: Comparing strings (2) | Home | Next: Palindromic squares

A "palindrome" is a word (or sentence) whose letters are the same forward as backward. Words like "kayak," "rotor," and "dad" are palindromes. There are palindromic phrases too, like "was it a car or a cat i saw." Typically, spaces and punctuation are ignored.

Let's write a program that will tell you if a word (or phrase) is a palindrome. Here's the needed logic:
  1. Read a string and convert it to lowercase for a "safe" comparison.
  2. Remove the spaces from the string
  3. Compare the original string to the reverse space-free string and see if they match.


Here are the three string functions we'll use:
string.sub(string,start,end)
and
string.reverse(string)
Move the mouse over a dotted box for more information.

Now you try. See if you can fix the if statement to test if string b is equal to its reversed version.

Type your code here:


See your results here: