C语言问题,我想请教一下各位,
从键盘读入一串字符,以 '!' 结束,分别统计其中数字0,1,2... 9出现的次数。
输入:1902d76k5910!
输出:The character 0 appears 2 times
The character 1 appears 2 times
The character 2 appears 1 times
The character 3 appears 0 times
The character 4 appears 0 times
The character 5 appears 1 times
The character 6 appears 1 times
The character 7 appears 1 times
The character 8 appears 0 times
The character 9 appears 2 times
点击右侧采纳即可:
#include <stdio.h>
int main() {
char c;
int count[10] = {0};
while ((c = getchar()) != '!') {
if (c >= '0' && c <= '9') {
count[c - '0']++;
}
}
for (int i = 0; i < 10; i++) {
printf("The character %d appears %d times\n", i, count[i]);
}
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: