计算各字符数
统计字符串中字母、数字、空格和其他字符的个数。 输出格式如下
第一行中输出“字母=x“
第二行中输出“数字=y“
第三行中输出“空格=z”,
第四行中输出“其他=m”
输入格式:
例如输入:“ab cd12 36e7$ @f 8g 9”
输出格式:
输出:
字母=7
数字=7
空格=5
其他=2
输入样例:
在这里给出一组输入。例如:
ab cd12 36e7$ @f 8g 9
输出样例:
在这里给出相应的输出。例如:
字母=7
数字=7
空格=5
其他=2
代码如下:哈哈哈哈!你懂的!
#include <stdio.h>
int letter,digit,space,others;
int main()
{
void count(char []);
char text[80];
gets(text);
letter=0;
digit=0;
space=0;
others=0;
count(text);
printf("字母=%d\n数字=%d\n空格=%d\n其他=%d",letter,digit,space,others);
return 0;
}
void count(char str[])
{
int i;
for(i=0;str[i]!='\0';i++)
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
letter++;
else if((str[i]>='0'&&str[i]<='9'))
digit++;
else if(str[i]==32)
space++;
else
others++;
}