练习5.20


编写一段程序,从标准输入中读取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;
}


posted @ 2014-12-25 15:26  Song_4  阅读(123)  评论(0编辑  收藏  举报