当i=2时正常执行循环内语句,但执行完之后就退出了,甚至都不执行i++。
把i<=strlen(pa)换成i<=len这个循环又正常了。
这是怎么回事?
代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char key[1000005],pa[10000005],word[1000005];
int main(){
gets(key);
gets(pa);
int f=0,count=0,index=-1,len=strlen(pa);
for(int i=0;i<=strlen(pa);i++){
if(pa[i]==' '||pa[i]=='\0'){
pa[i]='\0';
strcpy(word,&pa[f]);
if(strcasecmp(key,word)==0){
count++;
if(index==-1){
index=f;
}
}
f=i+1;
}
}
if(count==0){
cout<<-1;
}
else{
cout<<count<<' '<<index;
}
return 0;
}
输入:
To
to be or not to be is a question