come in come in
C语言:输入一行字符,分别统计出其中英文字母、空格、数字、和其他字符的个数。
要求:尽量少用或不用<ctype.h>等头文件包涵的函数。
////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <ctype.h>
void main()
{
char c;
int A_num=0,a_num=0,space=0,num=0,others=0;
while((c=getchar())!='\n')
{
if (isupper(c)) A_num++;
else if (islower(c)) a_num++;
else if (isdigit(c)) num++;
else if (isspace(c)) space++;
else others++;
}
printf("\这一行文字中:\n大写字母有%d个",A_num);
printf("\n小写字母有%d个",a_num);
printf("\n空格有%d个",space);
printf("\n数字有%d个",num);
printf("\n其它字符有%d个\n",others);
}
/////////////////////////////////////////////////////////////////
以上的代码使用了isupper,islower,isdigit,isspace四个函数。我的意思是避免使用诸如此类的函数。