如何统计在一篇文章中某个单词出现了几次,以及第一次出现的位置
发布时间:2020-12-14 23:54:38 所属栏目:C语言 来源:网络整理
导读:这篇文章提供的代码的作用就是对某个单词在文章中出现的次数进行统计。 实现代码: #includeiostream#includestring#includecstdiousing namespace std;void main(){ freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); string word,paper; getli
这篇文章提供的代码的作用就是对某个单词在文章中出现的次数进行统计。 实现代码: #include<iostream> #include<string> #include<cstdio> using namespace std; void main() { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); string word,paper; getline(cin,word); getline(cin,paper); short len1=word.size(); short len2=paper.size(); short i,sum(0); for(i=0;i<=len1-1;i++) { if(word[i]>=65&&word[i]<=90) word[i]+=32; } for(i=0;i<=len2-len1;i++) { if(paper[i]>=65&&paper[i]<=90) paper[i]+=32; if(paper[i]==word[0]) { short j; bool bo(1); for(j=1;j<=len1-1;j++) { if(paper[i+j]>=65&&paper[i+j]<=90) paper[i+j]+=32; if(paper[i+j]!=word[j]) bo=0; } if(bo==1) { sum++; if(sum==1) cout<<i<<' '; } } } cout<<sum<<endl; fclose(stdin); fclose(stdout); } 以上就是本文的全部内容,希望对大家的学习有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |