题目:
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 |
代码:
#include<stdio.h>
#include<stdlib.h>
void test()
{
//赋初值,否则系统随机赋值
char ch;
int A=0;
int B=0;
int C=0;
int D=0;
while((ch=getchar())!='\n')
{
//不打括号运算顺序是自左向右的,导致错误
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
A++;
}
else if(ch==' ')
{
B++;
}
else if(ch>='0'&&ch<='9')
{
C++;
}
else
{
D++;
}
}
//这句话很长,容易忘记写ABCD,导致系统随机赋值
printf("字母的个数是%d\n空格的个数是%d\n数字的个数是%d\n其它字符的个数是%d\n",A,B,C,D);
}
int main()
{
test();
system ("pause");
return 0;
}