若何統計在一篇文章中某個單詞湧現了幾回,和第一次湧現的地位。本站提示廣大學習愛好者:(若何統計在一篇文章中某個單詞湧現了幾回,和第一次湧現的地位)文章只能為提供參考,不一定能成為您想要的結果。以下是若何統計在一篇文章中某個單詞湧現了幾回,和第一次湧現的地位正文
這篇文章供給的代碼的感化就是對某個單詞在文章中湧現的次數停止統計。
完成代碼:
#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);
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助。