Problem G: 字符统计
Time Limit: 1 Sec Memory Limit: 16 MBSubmit: 1842 Solved: 1017
[ Submit][ Status][ Web Board]
Description
给出一篇英文文章,含大小写字母、数字、标点符号和空白符等,统计其中各个英文字母出现的次数和各类字符出现的次数。
统计各类字符时,应使用头文件<ctype.h>中的字符分类函数。这里用到以下5个函数:
islower(c) c是否小写字母:'a'~'z';
isupper(c) c是否大写字母:'A'~'Z';
isdigit(c) c是否数字:'0'~'9';
isspace(c) c是否空白字符:包括空格(' ')、换页符('\f')、换行符('\n')、回车符('\r')、水平制表符('\t')和垂直制表符('\v');
ispunct(c) c是否标点符号:标点符号包括除字母、数字和空白符之外的所有可打印字符,也就是说输入的所有字符减去以上4类字符数正好是标点符号数。(仅限C99)
注意,在C89中(如VC6.0),ispunct()函数的定义与C99(如本地的codeblocks和OJ后台的gcc)不同。在C89中,ispunct(c)在c为除空格符和字母、数字之外的所有可打印字符时都返回真。所以,当你使用VC6.0编译器时,使用该函数本地计算的结果可能不对,但是不影响提交后的结果正确,如果你的程序没有其他错误。
Input
输入为一篇英文文章,最少有一个字符,至EOF结束。
Output
输出各类字符的出现次数和每个字母的出现次数。按顺序,每行输出依次为:所有字符数、小写字母数、大写字母数、数字字符数、空白符数、标点符号数,然后是从A~Z所有字母出现的次数。每个字母的出现次数是大小写合并统计的。
输出格式见sample。
Sample Input
Sample Output
HINT
静下心来想想,你能用数组做些什么?来简化程序的编写。否则,这么多种类需要统计,只是定义单字母的变量还不够用呢。那就太麻烦了!另外,不要试图数清楚sample的各种字符数。因为最后一行末尾的回车没显示出来,并且文中有很多对齐的地方使用的是制表符'\t',而不是空格,所以,你只能依赖自己的编程能力和构造测试数据的能力。
Append Code
//这个2020个Characters大家不要信,我用word查的a一共就112个,2020不好查。提示:wa17%:主要问题是getchar这里用的gets,我当时定义的char a【10005】wa17%了,不知道开个几十万能不能成。
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char a;
int main()
{
//freopen("in.txt","r",stdin);
int f,l=0,i,zmlow=0,zmup=0,num=0,space=0,pun=0;
int b[92]={0};
while((a=getchar())!=EOF)
{
l++;
//for(i=0;i<f;i++)
{
if(islower(a))
{
zmlow++;
b[a-96]++;
}
else if(isupper(a))
{
zmup++;
b[a-64]++;
}
else if(isdigit(a)) num++;
else if(isspace(a)) space++;
else if(ispunct(a)) pun++;
}
//space++;
}
//space--;
//l--;
printf("All Characters : %d\n",l);
printf("Lowers : %d\n",zmlow);
printf("Uppers : %d\nDigits : %d\nSpaces : %d\nPuncts : %d\n",zmup,num,space,pun);
for(i=1;i<27;i++)
{
printf("%c : %d\n",i+64,b[i]);
}
return 0;
}
/**************************************************************
Problem: 1509
User: 201701060928
Language: C
Result: Accepted
Time:8 ms
Memory:748 kb
****************************************************************/