forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_14.cpp
55 lines (46 loc) · 1.47 KB
/
ex5_14.cpp
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
46
47
48
49
50
51
52
53
54
55
//!@Alan
//!
//! Exercise 5.14:
//! 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 <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string current_str, previous_str, max_str, current_repeating_str;
int current_repeating=0, max_repeating=0;
cout<<"Please Enter:\n";
while (cin>>current_str)
{
if(current_str.compare(previous_str) == 0)
{
current_repeating ++ ;
current_repeating_str = current_str;
}
else
{
current_repeating = 0;
current_repeating_str.clear();
}
if(current_repeating > max_repeating)
{
max_repeating = current_repeating;
max_str = current_repeating_str;
}
cout<<"The Max repeating string now is "
<<max_str
<<"\nThe repeating times="
<<max_repeating
<<std::endl;
previous_str = current_str;
}
return 0;
}