In Figure 7.3, we presented a program that checks a string to see if the letters in
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. 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 Answer:
Heres an implementation in Python of the program described in the problem statement import string de...View the full answer
Students also viewed these Computer science questions
-
Test the following function to determine whether or not it is a binomial probability function. List the distribution of probabilities and sketch a histogram. Tr)-C)(1).(1 5-x for x = 0, 1, 2, 3, 4, 5
-
For each of the following, tell whether or not you would expect it to have a strong seasonal component and why. a. Sales of colorful wrapping paper, recorded monthly. b. The number of air travelers...
-
For each item listed, state whether or not it would be disclosed for each of the reportable segments, identify the segments for which it would be disclosed and explain what other disclosures, if any,...
-
The accompanying data are consistent with summary statistics that appeared in the paper Shape of Glass and Amount of Alcohol Poured: Comparative Study of Effect of Practice and Concentration (...
-
A sample of 20 chocolate chip cookies was taken from a box. The cookies were examined for the number of chocolate chips in each cookie. The numbers recorded were 18 15 17 17 16 18 16 15 16 14 16 17...
-
The intensity of sunlight at the top of the earth's atmosphere is approximately \(1400 \mathrm{~W} / \mathrm{m}^{2}\). Mars is about 1.5 times as far from the sun as the earth. What is the...
-
What are the primary advantages of resolving a case through alternative dispute resolution methods rather than going to trial? In what situations might a trial be preferable?
-
Organic Grains LLC uses statistical process control to ensure that its health- conscious, low- fat, multigrain sandwich loaves have the proper weight. Based on a previously stable and in- control...
-
a) Explain the following accounting concepts: i. Business entity Revenue realization ii. Accruals iii. iv. V. Substance over form Conservatism vi. Going concern (6 Marks) b) Classify the following...
-
What characteristic does Ruby share with Smalltalk?
-
Heres a new idea for implementing the sequence class from Section 4.5. Instead of the items being stored on a linked list, they will be stored using two stacks as private member variables with the...
-
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...
-
At what angle should a ball be thrown so that it has the maximum range? Ignore air drag.
-
What are the advantages and disadvantages of using virtual reality interface for interaction?
-
Homer Simpson finds himself in a panic attempting to meet the April 15th tax deadline. If Homer and his wife have an income of $60,000, earned $1200 in interest, invested $2500 in a tax-deferred...
-
Jonas Company has the following information related to its manufacturing and selling of decorative vases. What are the variable costs and fixed costs that Jonas Company will use to calculate the...
-
What is the DECIDE framework for evaluation and how does it facilitate advanced assessment processes?
-
Examine the architectural considerations and performance optimizations involved in designing operating systems for modern heterogeneous computing platforms, including support for specialized hardware...
-
Williams & Sons last year reported sales of $12 million, cost of goods sold (COGS) of $10 million, and an inventory turnover ratio of 2. The company is now adopting a new inventory system. If the new...
-
Draw the appropriate control flow graph of the given pseudocode.Make sure to only use one number for blocks of code which are all sequential and when the first line is executed, all of those lines...
-
What is the largest integer m such that every simple connected graph with n vertices and n edges contains at least m different spanning trees? A. 1 B. 2 C. 3 D. n
-
How does depth first traversal work?
-
Let G be a weighted undirected graph and e be an edge with maximum weight in G. Suppose there is a minimum weight spanning tree in G containing the edge e. Which of the following statements is always...
-
Organizations in the construction industry monitor interest and unemployment rates. Any dramatic increases in these rates will negatively impact their industry. This is an example of what element of...
-
Emails and 24/7 chatbot system are two common forms of two-way addressable media used by Shoppee to sustain a learning relationship with customers. Let's examine how successfully these two media are...
-
Real estate development company normally set up investment committees who responsibles for making the investment decisions. as a member of the committee, and have the responsability for making sound...
Study smarter with the SolutionInn App