本文的主要內(nèi)容就是統(tǒng)計(jì)某個(gè)單詞在一篇文章中出現(xiàn)了幾次,以及第一次出現(xiàn)的位置,需要的朋友可以參考下
這篇文章提供的代碼的作用就是對(duì)某個(gè)單詞在文章中出現(xiàn)的次數(shù)進(jìn)行統(tǒng)計(jì)。
實(shí)現(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);
- }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
新聞熱點(diǎn)
疑難解答