Skip to content

Instantly share code, notes, and snippets.

@id-0x76adf1
Created December 26, 2012 01:47
Show Gist options
  • Select an option

  • Save id-0x76adf1/4377108 to your computer and use it in GitHub Desktop.

Select an option

Save id-0x76adf1/4377108 to your computer and use it in GitHub Desktop.
Keep track of the largest number of times a single repetition occurs and which word is repeated.
#include <iostream>
#include <string>
int main(int arc, char* argv[])
{
int max_count = 0;
std::string max_count_word;
int count = 0;
std::string prev_word;
std::string curr_word;
std::cin >> curr_word;
prev_word = curr_word;
++count;
while (std::cin >> curr_word)
{
if (curr_word == prev_word)
{
++count;
}
else
{
if (count > max_count)
{
max_count = count;
max_count_word = prev_word;
prev_word = curr_word;
}
}
}
if (max_count > 1)
{
std::cout << max_count_word << " ocurrs " << max_count << " times" << std::endl;
}
else
{
std::cout << "no word was repeated" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment