编写程序,输入一字符串输出此字符串中字母、数字、空格和其他字符的个数。要求:
(1)编写一个函数实现上述统计功能:由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数。
(2)在主函数中输出上述统计结果。
提示:借助全局变量
#include<stdio.h>
#include<string.h>
int main()
{
char str[80];
int i;
int a = 0,d = 0, e = 0, f = 0;
char c;
gets(str);
for (i = 0; (c = str[i]) != '\0'; i++)
{
if ((c >= 'A' && c <= 'Z')||(c >= 'a' && c <= 'z'))
a++;
else if (c >= '0' && c <= '9')
d++;
else if (c == ' ')
e++;
else
f++;
}
printf("\n统计的字母为:%d个\n数字为:%d个\n空格为:%d个\n其它字符为:%d个\n", a, d, e, f);
return 0;
}