Home CPSC 220

Palindrome Checker



To get more practice with recursion.


For this lab, you will write a program that checks whether a particular string is a palindrome. A palindrome is a string that reads the same backwards as forwards. For example "madamImadam" "risetovotesir".

Typically spaces, capitalization and punctuation are ignored in palindromes, meaning "Madam I'm Adam" and "Rise to vote sir" would be considered palindromes. Your program does not need to take this into account, however, and only needs to check for exact palindromes.

You could do this with a loop, but instead you should use the following recursive algorithm:

  1. If the string is 0 or 1 in length, then return true. This is the base case.
  2. If the first and last character are different, then return false.
  3. Otherwise, recursively check for the rest of the string, not including the first and last character.


Example Runs

Enter a phrase: madamImadam
Yes, that is a palindrome!

Enter a phrase: abccba
Yes, that is a palindrome!

Enter a phrase: abcaa
No, that is not a palindrome!





When your program works, email the code to ifinlay@umw.edu.

Copyright © 2022 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.