输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。
(1)定义变量接收输入; (2)定义特定变量; (3)循环计算; (4)多分支结构判断; (5)输出结果;
#include <stdio.h>
int main()
{
char input;
int word = 0, num = 0, blank = 0, other = 0;
printf("please enter the character and press enter to confirm:");
for (; (input = getchar()) != '\n';) //注意括号位置
{
if ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z'))
word++;
else if (input >= '0' && input <= '9')
num++;
else if (input == ' ')
blank++;
else
other++;
}
printf("word=%d,num=%d,blank=%d,other=%d\n", word, num, blank, other);
return 0;
}