#include <stdio.h>
main()
{
int c, i, nwhite = 0, nother = 0;
int ndigit[10];
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while( (c = getchar()) != EOF)
if (c >= '0' && c <= '9')
ndigit[c - '0']++;
else if (c == ' ' || c == '\n' || c == '\t')
nwhite++;
else
nother++;
printf("digits =");
for (i = 0; i < 10; i++)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
这段代码里,判断输入的字符是否为数字时,为什么不能去掉‘0’和‘9’的单引号?以及下面c既然已经是数字了,为什么不可以直接写ndigit[c],而要写成ndigit[c - '0'] ?试了一下好像确实不可以,但不到是为什么。萌新求教。