读取一个文件中的字符,统计每个字符出现的次数
生活随笔
收集整理的這篇文章主要介紹了
读取一个文件中的字符,统计每个字符出现的次数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 //統(tǒng)計(jì)每個字符出現(xiàn)的次數(shù)
2 #include<stdio.h>
3 #include<stdlib.h>
4 #include<string.h>
5
6 int main()
7 {
8 FILE *fp_read;
9 char ch;
10 int count[26];
11 int index;
12 fopen_s(&fp_read,"E:\\first.txt","r");
13 memset(count,0,sizeof(count));
14 if(NULL == fp_read)
15 {
16 printf("Can not open the file!\n");
17 system("pause");
18 return 0;
19 }
20
21 while(!feof(fp_read))
22 {
23 ch = fgetc(fp_read);
24 if(ch >= 'a' && ch <= 'z')
25 count[ch - 'a']++;
26 }
27 for(index = 0; index < 26; index++)
28 if(count[index] != 0)
29 printf("%c:\t%d\n",index+97, count[index]);
30
31 fclose(fp_read);
32
33 system("pause");
34 return 0;
35 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/cpsmile/p/4776870.html
總結(jié)
以上是生活随笔為你收集整理的读取一个文件中的字符,统计每个字符出现的次数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为啥泰山对中国文化的影响如此深远?
- 下一篇: 长跑(耐力运动)