summaryrefslogtreecommitdiff
path: root/5p14.cpp
blob: 5c032e14368a13523940226ffec17e006593a745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

/*
 *
 * 5.14
 *
 * The program does not handle cases where words repeat for
 * the same amount of times. So for example if we input
 * "hello hello hi hi" only hello would print as the top word
 */

int main () {

	std::string NewWord;
	std::string TempWord;
	std::string MostOccuring;
	uint32_t TmpCount = 0;
	uint32_t TopCount = 0;
	while(std::cin >> NewWord) {
		if(TempWord != NewWord) {			
			if(TmpCount > TopCount) {
				TopCount = TmpCount;
				MostOccuring = TempWord;				
			}

			TempWord = NewWord;
			TmpCount = 1;
		} else if(TempWord == NewWord) {
			++TmpCount;
			if(TmpCount > TopCount) {
				TopCount = TmpCount;
				MostOccuring = TempWord;
			}
		}
	}

	
	if(TopCount > 1) {
		std::cout << MostOccuring << " is the top word occuring " << TopCount << " times." << std::endl;
	} else {
		std::cout << "No words were repeated!" << std::endl;
	}
	
	return 0;
}