Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给出一串字符,要求统计出里面的字母、数字、空格以及其他字符的个数。
字母:A, B, …, Z、a, b, …, z组成
数字:0, 1, …, 9
空格:" "(不包括引号)
剩下的可打印字符全为其他字符。
Input
测试数据有多组。
每组数据为一行(长度不超过100000)。
数据至文件结束(EOF)为止。
Output
每组输入对应一行输出。
包括四个整数a b c d,分别代表字母、数字、空格和其他字符的个数。
Sample Input
A0 ,
Sample Output
1 1 1 1
Hint
Source
ZJGSU
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n,i;
char a[100003];
while(gets(a)!=NULL)
{
int a1 = 0,a2 = 0,a3 = 0,a4 = 0; /*注意:因为是多组输入,
所以计数变量每一次都要初始化;*/
n = strlen(a);
for(i=0;i<n;i++)
{
if((a[i]<='z'&&a[i]>='a')||(a[i]<='Z'&&a[i]>='A'))
a1++;
else if(a[i]<='9'&&a[i]>='0')
a2++;
else if(a[i]==' ')
a3++;
else a4++;
}
printf("%d %d %d %d\n",a1,a2,a3,a4);
}
return 0;
}
运行结果:
A0 ,
1 1 1 1
^Z
Process returned 0 (0x0) execution time : 8.660 s
Press any key to continue.