A palindrome is a piece of text which reads the same forwards as backwards. Characters such as spaces, punctuation marks, etc. are usually ignored in performing a test for a palindrome, and the case of letters is disregarded. For example, the following are all palindromes:
Anna
Madam I'm Adam
Able was I ere I saw Elba
Draw, o coward!
"Delia was ill!" Lisa wailed
Your task this week is to write a Java program that will read in lines of text and indicate whether or not they are palindromes. The program should terminate when a blank line is entered.
To make the assignment a little more challenging, your program should contain two versions of a method for testing whether a string is palindromic:
Iterative: Use a loop to compare the first character with the last, the second with the next-to-last, etc. Think carefully about efficiency (e.g. do not simply reverse the string and compare with the original).
Recursive: The method makes calls to itself to analyse the string.
Invoke each of the two methods for every string entered by the user. The following shows a possible session you might obtain by running your program (user input in bold):
$ java Palindrome
Enter a line of text:
Anna
Iterative version: This is a palindrome
Recursive version: This is a palindrome
Enter a line of text:
Anne
Iterative version: This is not a palindrome
Recursive version: This is not a palindrome
Enter a line of text:
Madam I'm Adam
Iterative version: This is a palindrome
Recursive version: This is a palindrome
Enter a line of text:
"Delia was ill!" Lisa wailed.
Iterative version: This is a palindrome
Recursive version: This is a palindrome
Enter a line of text:
abcdefg
Iterative version: This is not a palindrome
Recursive version: This is not a palindrome
Enter a line of text:
Program finished