Read strings from a standard input, and find the most duplicated word Write a program to read strings from standard input looking for duplicated words.
The program should find places in the input where one word is followed immediately by itself.
Keep track of the largest number of times a single repetition occurs and which word is repeated.
Print the maximum number of duplicates, or else print a message saying that no word was repeated.


For example, if the input is

how now now now brown cow cow

the output should indicate that the word now occurred three times.

#include &lt;iostream&gt;
#include &lt;string&gt;

int main()
{
    using std::cin; using std::cout; using std::endl;
    using std::string;

    // running median
    string most_duplicated_str;
    int most_duplicated_count = 0;

    // loop variables
    string prev_str;
    string new_str;
    int new_duplicate_count = 1;

    while(cin &gt;&gt; new_str)
    {
        if (new_str != prev_str)
        {
            // check and update median
            if (new_duplicate_count &gt; most_duplicated_count)
            {
                most_duplicated_str = prev_str;
                most_duplicated_count = new_duplicate_count;
            }

            // update prev_str
            new_duplicate_count = 1;
            prev_str = new_str;
            continue;
        }

        ++new_duplicate_count;
        prev_str = new_str;
    }

    // check and update median for last word
    if (new_duplicate_count &gt; most_duplicated_count)
    {
        most_duplicated_str = prev_str;
        most_duplicated_count = new_duplicate_count;
    }

    // print result
    if(most_duplicated_count == 0)
        cout &lt;&lt; &quot;# No Duplicated Words #\n&quot;;
    else
        cout &lt;&lt; &quot;Most duplicated word: &quot; &lt;&lt; most_duplicated_str &lt;&lt; &quot;, &quot; &lt;&lt; most_duplicated_count &lt;&lt; &quot; times.\n&quot;;
}
Nothing awful here.
If this was production code then putting things in named functions would be nice to help with self documenting code.

Don't particularly like this.
    using std::cin; using std::cout; using std::endl;
    using std::string;

But I am not going to complain about it very much.

Using snake case is unusual but not unheard of in C++.
    string most_duplicated_str;
    int most_duplicated_count = 0;

Like the long descriptive names and the grouping of you variables into logical combinations.

I would note that:
      cin &gt;&gt; new_str

Reads a white space separated word. For college and learning purposes this will be fine. But in real life you would have to consider punctuation and other non alphabetical content and what to do with it.

Not a very useful comment.
I would either make it more descriptive (more words) or remove it.
    // loop variables

and
            // update prev_str


These three lines can be simplified:
            new_duplicate_count = 1;
            prev_str = new_str;
            continue;

to:
            new_duplicate_count = 0;

It will have the same effect and the loop will feel more logical as the same action happens every loop. But that's just my opinion on readability. If you did not change it I would not oppose it being merged into mainline.

If any words are read: Can the value ever be zero?
    // print result
    if(most_duplicated_count == 0)
        cout &lt;&lt; &quot;# No Duplicated Words #\n&quot;;
    else

If you only have a duplicate count of 1 were there any duplicates?
What happens if there are ties in the number of duplicate counts?
These are good questions to ask your customer! :-)