Question: C++ problem: The program shall read, from standard input, an arbitrary number of whitespace-delimited tokens (i.e., words ) . You may assume that the number

C++ problem:

The program shall read, from standard input, an arbitrary number of whitespace-delimited tokens (i.e., words). You may assume that the number of words does not exceed the capacity of the int type on our server.

A word shall be considered a palindrome if:

  1. Its length is greater than 1.

  2. Its sequence of characters is the same forward and backward, without regard to case. (e.g., the name Otto shall be considered a palindrome.)

The program shall print, to standard output, two or three lines, in the following order, summarizing the input:

  1. A line containing the total number of words, printed as a decimal integer

  2. A line containing the total number of palindromes, printed as a decimal integer

  3. A line containing the longest palindrome (i.e., the palindrome with the greatest length)

    1. The palindrome shall be printed in lowercase.

    2. If there is a tie for longest, choose the first occurrence.

    3. This third line shall be printed if and only if the input contains one or more palindromes

    4. The format of your output lines can be free-form, but make sure that the only decimal numbers contained in lines 1 and 2 are the relevant numbers of words and palindromes.

  4. You shouldn't have to do any string tokenization, since extracting a std::string from a std::istream (e.g. std::cin) automatically tokenizes by whitespace. The following construct should be enough 1):

    for (std::string word; std::cin >> word;) {
  5. Consider making all tokens lowercase. The most C++ way to do this is with the std::transform function from , which you're free to use2). Other ways are acceptable as well.

Sample output:

Command: cs19_longest_palindrome <<< "You might be a Nauruan if you're from Nauru."
Output 9 words 1 palindrome Longest palindrome: nauruan
Command: cs19_longest_palindrome < /srv/datasets/shakespeare-othello.txt

output:

27395 words 88 palindromes Longest palindrome: madam

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!