如何统计在一篇文章中某个单词出现了几次,以及第一次出现的位置

本文的主要内容就是统计某个单词在一篇文章中出现了几次,以及第一次出现的位置,需要的朋友可以参考下

这篇文章提供的代码的作用就是对某个单词在文章中出现的次数进行统计。

实现代码:

 #include #include #include 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<

以上就是本文的全部内容,希望对大家的学习有所帮助。

以上就是如何统计在一篇文章中某个单词出现了几次,以及第一次出现的位置的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言