c++输出连续出现的单词数
#include <iostream>
#include <unistd.h>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
string word,tmp,word_tgt;
unsigned int cnt = 0,cntmax = 0;
while(cin>>word)
{
if(tmp == word)//c++里可以用==直接比较两个词,c里就得用strcmp()函数
{
cnt++;
if(cnt>cntmax)
{
cntmax= cnt;
word_tgt = word;
}
}
else
{
tmp = word;
cnt = 1;
}
}
if(cnt != 1)
cout<<word_tgt<<" appeared "<<cntmax<<"times."<< endl;
else
cout<<"There are no consecutive words."<<endl;
sleep(6);
}