A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes:
• warts n straw
• radar
• Able was I ere I saw Elba
• xyzczyx
Write a program that will accept a sequence of characters terminated by a period and will decide whether the string—without the period—is a palindrome. You may assume that the input contains only letters and blanks and is at most 80 characters long. Include a loop that allows the user to check additional strings until she or he requests that the program end. Hint: Define a static method called isPalindrome that begins as follows:
/**
Precondition: The array a contains letters and
blanks in positions a[0] through a[used ? 1].
Returns true if the string is a palindrome and
false otherwise.
*/
public static boolean isPalindrome(char[] a, int used)
Your program should read the input characters into an array whose base type is char and then call the preceding method. The int variable used keeps track of how much of the array is used, as described in the section entitled “Partially Filled Arrays.”
The solution to this project reads in a line of text using the String class, then passes the string to the palindrome method which performs the test, returning true if the phrase is a palindrome, or false if not. A note in the source code's prologue calls attention to its behavior in the degenerate case where the phrase has no letters (either all blanks or a null String). Given the requirements in the problem statement that the palindrome method is passed the original line of text and returns a Boolean value, it cannot return an indication that the file was empty. As the note in the prologue states, it returns true in these situations. The palindrome method uses a character array to parse the array, getting rid of white spaces and saving only the letters (actually, it saves anything other than white space characters). Then it analyzes the letters to see if it a palindrome. The parsing technique can be borrowed from Project 2, so the interesting part of this problem is figuring out the algorithm to get the correct characters to compare. The algorithm is described in comments in the palindrome code. An easy way to make the palindrome test insensitive to case (it should ignore whether the characters are upper or lower case) is to make all the characters the same case, either upper or lower. The solution shown here uses the toUpperCase method in the Character wrapper class, but it would be equally correct to use toLowerCase.
See the code in Palindrome.java.
You might also like to view...
By default, how often does PowerPoint save your document?
A) 15 minutes B) 8 minutes C) 10 minutes D) 6 minutes
To reposition the insertion point using the mouse, move the mouse to the position where you want the insertion point to appear, and then click the left mouse button.
Answer the following statement true (T) or false (F)