Palindrome Checker
Objective
To get more practice with recursion.Task
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:
- If the string is 0 or 1 in length, then return true. This is the base case.
- If the first and last character are different, then return false.
- 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!
Details
- You should write a recursive function that takes a String as parameter and returns a boolean.
- Your function cannot use a loop (no for, while or do/while allowed!).
- You can use the "charAt" string method to check the character at a specific index, and the "length" method to find the length of the string.
- You can also use the "substring" method to obtain the string without first and last characters.