在上一篇的基础上修改,使得只寻找以大写字母开头的连续出现的单词:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string curword,preword;
cout<<"please enter:"<<endl;
while (cin>>curword)
{
if (preword==curword && isupper(curword[0]))
break;
else
preword=curword;
}
if (preword==curword && !curword.empty())
{
cout<<"the repeat word is:"<<curword<<endl;
}
else
cout<<"no repeat"<<endl;
return 0;
}
用函数isupper()来判断是否是大写字母。注意要包含头文件#include<cctype>。