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:
-
Its length is greater than 1.
-
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:
-
A line containing the total number of words, printed as a decimal integer
-
A line containing the total number of palindromes, printed as a decimal integer
-
A line containing the longest palindrome (i.e., the palindrome with the greatest length)
-
The palindrome shall be printed in lowercase.
-
If there is a tie for longest, choose the first occurrence.
-
This third line shall be printed if and only if the input contains one or more palindromes
-
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.
-
-
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;) { - 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
Get step-by-step solutions from verified subject matter experts
