Question: In Figure 7.3, we presented a program that checks a string to see if the letters in the string read the same forward and backward.
In Figure 7.3, we presented a program that checks a string to see if the letters in the string read the same forward and backward. These strings are called palindromes. Another kind of palindrome is one in which we look at words rather than letters. A word-by-word palindrome is a string of words such that the words read the same forward and backward. For example, the quote “You can cage a swallow, can’t you, but you can’t swallow a cage, can you?” is a word-byword palindrome. Write a program to test an input string and tell whether or not it is a word-by-word palindrome. Consider upper- and lowercase letters to be the same letter. Define a word as any string consisting of only letters or an apostrophe and bounded at each end with one of the following: a space, a punctuation mark, the beginning of the line, or the end of the line. Your program should have a friendly interface and allow the user to check more lines until the user wishes to quit the program.


public static boolean is_palindrome (String input) // The return value is true if and only if the input string is a palindrome. // All non-letters are ignored, and the case of the letters is also ignored. // See page 365 for an explanation of using Java's LinkedList class as a queue. { Queue q = new LinkedList ( ); Stack s new Stack ( ); Character letter; int mismatches %3D // One character from the input string 0; // Number of spots that mismatched // Index for the input string int i; for (i = 0; i < input.length( ); i++) { letter = input.charAt (i); if (Character.isLetter(1etter)) { q. add (letter); s.push(letter); } } %3D while (!q.isEmpty( )) { if (q.remove (O !- s.pop( )) mismatches++; } // If there were no mismatches, then the string was a palindrome. return (mismatches = 0); } }
Step by Step Solution
3.50 Rating (163 Votes )
There are 3 Steps involved in it
Heres an implementation in Python of the program described in the problem statement import string de... View full answer
Get step-by-step solutions from verified subject matter experts
