蒟蒻继续打新手村。。。


#include<iostream> #include<cstring> #include<cstdio> using namespace std; string sent,word; int lens,lenw,t,pos; int main() { getline(cin,word); getline(cin,sent); //!!!!!!!字符串含有空格输入,不可以用cin,否则to me只能读到to lenw=word.size(); // 不能用s1.lenth()。。。 lens=sent.size(); for(int i=0;i<lenw;i++) word[i]=toupper(word[i]); //转成大写 for(int i=0;i<lens;i++) sent[i]=toupper(sent[i]); t=0; for(int i=0;i<=lens-lenw;i++) { int j; for(j=0;j<lenw;++j) { if(sent[j+i]!=word[j]) break; if(i>0&&sent[i-1]!=' ') break; //前一个不是空格则终止 } if(j==lenw&&(sent[j+i]==' '||j+i==lens)) //单词长度匹配,后面是空格或者句末 { t++; //目的:为了输出单词第一次出现的位置 if(t==1) pos=i;          // 学到了。。。 } } if(t==0)cout<<-1; else cout<<t<<" "<<pos; return 0; }

猜你喜欢

转载自www.cnblogs.com/poo7/p/11689394.html
今日推荐