使用continue判断重复单词以大写开头
很重要的一个细节就是:
即使是那些非大写单词由于他们被输入过,所以迭代时候要注意
后值要传给前值;
对于字符串使用cin输入时是有风险的
比如你想输入的是 “ Li LEI” 中间含间隔
然而使用cin读取字符串的时候,却把他们分成两个字符串 。。。
个人觉得字符串 使用 getline(cin,变量) 来整行输入好些。。
毕竟字符串不同于其他变量
使用getline(cin,变量)读取整行字符串的代码
切记一行一字符串,输入后需回车;
#include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
string i="",j="";
int cnt=;
while(getline(cin,j)&&!j.empty())
{
if(j[]<||j[]>)
{
i=j;
cnt=;
continue;
}
if(j!=i)
{
i=j;
cnt=;
}
else
{
++cnt;
cout<<"连续重复出现两次的字符串是:"<<j<<endl;
break;
}
}
if(cnt==)
cout<<"没有字符串重复出现过"<<endl;
return ;
}
当输入 GTV V
GTV V
以下是使用cin输入的
#include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
string i="",j="";
int cnt=;
while(cin>>j&&!j.empty())
{
if(j[]<||j[]>)
i=j;
cnt=;
continue;
if(j!=i)
{
i=j;
cnt=;
}
else
{
++cnt;
cout<<"连续重复出现两次的字符串是:"<<j<<endl;
break;
}
}
if(cnt==)
cout<<"没有字符串重复出现过"<<endl;
return ;
}