【发布时间】:2021-12-27 05:53:29
【问题描述】:
我正在尝试查找输入中数字的出现次数(键入的数字)。我对表达式 ++ndigit[ c - '0'] 中数组的下标表达式感到困惑。
这里是代码
#include <stdio.h>
/* count digits, white space, others*/
main() {
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for(i = 0; i < 10; ++i)
{ndigit[i] = 0;}
while ((c = getchar()) I= EOF)
{if (C >= '0' && C <= '9')
{++ndigit[c-'0'];}
/*here i stucked explain me how
subscript
expression going too work*/
elseif (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);
}
【问题讨论】:
-
首先,你的代码中有很多格式和语法问题。是这个问题,还是逻辑本身?
-
while ((c = getchar()) I= EOF)???
标签: arrays c expression subscript