练习5.20

来源:互联网 发布:js身份证格式校验 编辑:程序博客网 时间:2024/05/16 09:03

编写一段程序,从标准输入中读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完位置。使用while循环一次读取一个单词,当一个单词连续出现两次时使用break语句终止循环。输出连续重复出现的单词,或者输出一个消息说明没有任何单词是连续重复出现的。

#include <iostream>#include <string>using namespace std;int main(){    string str1;    string str;    int count=0;    if(cin>>str)        str1=str;    while(cin>>str){        if(str==str1){            cout<<str<<endl;            count++;            break;        }        else            str1=str;    }    if(count==0)        cout<<"无重复"<<endl;    return 0;}


0 0