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...
-
Write a complete equation for neutron-induced fission of plutonium-239 that yields barium-143, two neutrons, and another nucleus.
-
A study analyzed the sleep habits of college students. The study found that the mean sleep time was 6.8 hours, with a standard deviation of 1.4 hours. Random samples of 100 sleep times are drawn from...
-
Nelson Company provides the following information for the month ended October 31, 2017: sales on credit $280,000, cash sales $95,000, sales discounts $5,000, and sales returns and allowances $11,000....
-
A Treasury bond with maturity exactly 18 years and 5 months and coupon 5.0 percent trades at an yield to maturity (YTM) of 3.3 percent (BEY). What is the value of the bond (and, therefore, its dirty...
-
Write a Super class interface employee has name and id number. Write manager and labour derived from employee class. Manager class has member data qfunction and qualification and manager allowance...
-
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...
-
Great Value Optical Company reported the following amounts on its balance sheet at December 31, 2018 and 2017: Great Value Optical Company reported the following amounts on its balance sheet at...
-
What is the best type of message to reach todays consumer?
-
What are the four different market segments? Why is it important to define and understand each?
-
Describe how factors shaping your identity contribute to health outcomes.
-
What are the benefits of having a two-way dialogue between buyer and seller?
-
Explain why theory is used in health communication planning, citing a specific example from the chapter.
-
On September 1, 2016, Marsha sells stock in Orange, Inc., for $90,000. The stock is specialized small business investment company (SSBIC) stock and was purchased on August 16, 2015, for $60,000. On...
-
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...
-
A farmer wonders if his crops grow better in sun or in shade. He measures the amount of fruit gathered from a sample of 50 trees growing in full sun and from a sample of 50 trees growing in mostly...
-
Consider the diagram. Determine the length of AC to the nearest cm. Determine the length of DC to the nearest cm. 28 38 A 224 cm 789 B
-
Use this picture to answer the following questions. 10 18 16. 3 AB=10 AC=16 AD=22 AE=15 BC-17 BD=18 15 BE=25 CD=26 CE=8 DE=11 Find the Nearest Neighbor Circuit for each vertex. Also give the weight...
Study smarter with the SolutionInn App