Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 5.14.cpp #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions ch5/5.14.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
#include <iostream>
#include <string>

int main() {
std::string word, duplicatedWord, maxDuplicatedWord;
unsigned cnt = 0, maxCnt = 0;
while (std::cin >> word) {
if (word == duplicatedWord)
++cnt;
else {
if (cnt > maxCnt) {
maxDuplicatedWord = duplicatedWord;
maxCnt = cnt;
}
duplicatedWord = word;
cnt = 1;
}
}
if (cnt > maxCnt) {
maxDuplicatedWord = duplicatedWord;
maxCnt = cnt;
}
if (maxCnt > 1) {
std::cout << maxDuplicatedWord << " occurs " << maxCnt
<< " times." << std::endl;
} else {
std::cout << "No word was repeated." << std::endl;
}
return 0;
int main()
{
int repetition = 1;
int occur = 1;
std::string largest;
std::string s;
std::string temp;

while (std::cin >> s)
{
if (temp != s)
{
temp = s;
}
else
{
++occur;
if (occur > repetition)
{
repetition = occur;
largest = s;
}
}
}
if (repetition > 1)
std::cout << largest << "occured: " << repetition;
else
std::cout << "no repetiotion";

return 0;
}