Problem F: 零起点学算法101——统计字母数字等个数
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 19810 Solved: 7601
Description
输入一串字符,统计这串字符里的字母个数,数字个数,空格字数以及其他字符(最多不超过100个字符)
Input
多组测试数据,每行一组
Output
每组输出一行,分别是字母个数,数字个数,空格字数以及其他字符个数
Sample Input
I am a student in class 1.
I think I can!
Sample Output
18 1 6 1
10 0 3 1
HINT
char str[101];//定义字符型数组
while(gets(str)!=NULL)//多组数据
{
//输入代码
for(i=0;str[i]!='\0';i++)//gets函数自动在str后面添加'\0'作为结束标志
{
//输入代码
}
//字符常量的表示, 'a'表示字符a; '0'表示字符0;
//字符的赋值 str[i]='a';
//表示将字符a赋值给str[i] str[i]='0';
//表示将字符0赋值给str[i]
}
#include <stdio.h>
int main()
{
int a,b,c,d;
char str[110];
//gets()函数把一行字符存入str中 ,最后的回车符会被舍弃
while (gets(str) != NULL)
{
a = b = c = d = 0;
int i = 0;
while (str[i] != '\0')
{
if (('A'<=str[i] && str[i]<='Z')||('a'<=str[i] && str[i]<='z'))
a++;
else if ('0' <= str[i] && str[i] <= '9')
b++;
else if (str[i] == ' ')
c++;
else d++;
i++;
}
printf("%d %d %d %d\n",a,b,c,d);
}
return 0;
}