统计字符数
输入一组字符,要求分别统计出其中英文字母、数字、空格以及其他字符的个数。
判断输入字符是否为回车,若不是则执行循环体语句判断是英文字母、空格、数字、其他字符中的哪种,是哪种响应的变量值加1。
main()
{
char c; /定义c为字符型 /
int letters = 0, space = 0, digit = 0, others = 0; /定义letters、space、digit、others 4个变量为基本整型 /
printf("please input some characters\n");
while ((c = getchar()) != '\n') /当输入的不是回车时执行while循环体部分 /
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
letters++; /当输入的是英文字母时变量letters加1/
else if (c == ' ')
space++; /当输入的是空格时变量space加1/
else if (c >= '0' && c <= '9')
digit++; /当输入的是数字时变量digit加1/
else
others++; /当输入的既不是英文字母又不是空格或数字时变量others加1/
}
printf("char=%d space=%d digit=%d others=%d\n",letters,space,digit,others); /将最终统计结果输出 /
}