Skip to content

Commit

Permalink
Merge pull request Mooophy#28 from AlexHuang1734/master
Browse files Browse the repository at this point in the history
Solve a bug that the original code failed to handle consecutively identical input.
  • Loading branch information
Mooophy committed Oct 31, 2014
2 parents 419ebfd + cfb5423 commit b92eeae
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ch05/ex5_14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ int main()
int count = 0;
for (string str, prestr; cin >> str; prestr = str)
{
if (str == prestr) { ++count; continue; }
else if (count > max_duplicated.second) max_duplicated = {prestr, count};
count = 0;
if (str == prestr) { ++count;}
else count = 1;

if (count > max_duplicated.second && count >= 2) {
max_duplicated = { prestr, count };
}
}

if (max_duplicated.first.empty()) cout << "There's no duplicated string." << endl;
else cout << "the word " << max_duplicated.first << " occurred " << max_duplicated.second+1 << " times. " << endl;
else cout << "the word " << max_duplicated.first << " occurred " << max_duplicated.second << " times. " << endl;

return 0;
}

0 comments on commit b92eeae

Please sign in to comment.