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. The previous exercise performed a similar check using words in place of letters. In this exercise, you are to write a program that runs a similar check using lines rather than words or letters.

Write a program that reads in several lines of text and decides if the passage reads the same, whether you read the lines top to bottom or bottom to top; this is yet another kind of palindrome. For example, the following poem reads the same from top to bottom and from bottom to top: 

As I enter my time machine or maybe not,

I wonder whether free will exists.

Change?

Change!

I wonder whether free will exists—maybe not

as I enter my time machine, or ...

Consider upper- and lowercase versions of a letter to be the same letter. Consider word boundaries to be significant, so for example, the words in the first line must read the same as the words in the last line in order to pass the test (as opposed to just the letters reading the same). However, consider all word delimiters as being equivalent; i.e., a punctuation mark, any number of spaces, or any combination of these are all considered to be equivalent. The end of the passage should be marked by a line containing only the word “end,” spelled with any combination of upper- and lowercase letters and possibly with blanks before and/or after it. Your program should have a friendly interface and allow the user to check more passages until the user wishes to quit the program.

FIGURE 7.3 A Program to Recognize Palindromes Java Application Program // FILE: Palindrome.java // This program reads strings from the keyboard and determines whether each input // line is a palindrome. The program ignores everything except alphabetic letters, and it // ignores the difference between upper- and lowercase letters. import

FIGURE 7.3 A Program to Recognize Palindromes Java Application Program // FILE:

FIGURE 7.3 A Program to Recognize Palindromes Java Application Program // FILE: Palindrome.java // This program reads strings from the keyboard and determines whether each input // line is a palindrome. The program ignores everything except alphabetic letters, and it // ignores the difference between upper- and lowercase letters. import java.util.LinkedList; // See page 365 for why we use a Linked List. import java.util.Queue; import java.util.Stack; import java.util.Scanner; // Provides the Queue interface // Provides the Stack class // Provides the Scanner class (see Appendix B) public class Palindrome { public static void main (String[ ] args) { Scanner stdin = new Scanner(System.in); // Keyboard input String line; // One input line do { System.out.print("Your expression (or return to end): "); line = stdin.nextLine(); if (is_palindrome (line)) System.out.println("That is a palindrome."); else System.out.println("That is not a palindrome."); } while (line.length( ) != 0); }

Step by Step Solution

3.49 Rating (176 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres a Java program that implements the functionality described above import javautilArrayList impo... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Data Structures and Other Objects Using Java Questions!