如何統(tǒng)計在一篇文章中某個單詞出現(xiàn)了幾次,以及第一次出現(xiàn)的位置
更新時間:2015年08月03日 16:01:55 作者:淡定的問道
本文的主要內(nèi)容就是統(tǒng)計某個單詞在一篇文章中出現(xiàn)了幾次,以及第一次出現(xiàn)的位置,需要的朋友可以參考下
這篇文章提供的代碼的作用就是對某個單詞在文章中出現(xiàn)的次數(shù)進行統(tǒng)計。
實現(xiàn)代碼:
#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);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
C/C++?Qt?TableDelegate?自定義代理組件使用詳解
TableDelegate自定義代理組件的主要作用是對原有表格進行調(diào)整,本文主要介紹了QT中TableDelegate?自定義代理組件的使用教程,感興趣的朋友可以了解一下2021-12-12

