这篇文章提供的代码的作用就是对某个单词在文章中出现的次数进行统计。
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#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); } |
以上就是本文的全部内容,希望对大家的学习有所帮助。