c语言中数字字符计数
1、
#include <stdio.h> int main(void) { int i, ch; int cnt[10] = {}; while((ch = getchar()) != EOF) { switch(ch) { case '0': cnt[0]++; break; case '1': cnt[1]++; break; case '2': cnt[2]++; break; case '3': cnt[3]++; break; case '4': cnt[4]++; break; case '5': cnt[5]++; break; case '6': cnt[6]++; break; case '7': cnt[7]++; break; case '8': cnt[8]++; break; case '9': cnt[9]++; break; } } puts("show the times."); for(i = 0; i < 10; i++) { printf("'%d' = %d ", i, cnt[i]); } return 0; }
2、
#include <stdio.h> int main(void) { int i, ch; int cnt[10] = {}; while((ch = getchar()) != EOF) { switch(ch) { case 48: cnt[0]++; break; case 49: cnt[1]++; break; case 50: cnt[2]++; break; case 51: cnt[3]++; break; case 52: cnt[4]++; break; case 53: cnt[5]++; break; case 54: cnt[6]++; break; case 55: cnt[7]++; break; case 56: cnt[8]++; break; case 57: cnt[9]++; break; } } puts("show the times."); for(i = 0; i < 10; i++) { printf("'%d' = %d ", i, cnt[i]); } return 0; }
3、
#include <stdio.h> int main(void) { int i, ch; int cnt[10] = {}; while((ch = getchar()) != EOF) { if(ch >= 48 && ch <= 57) { cnt[ch - 48]++; } } puts("show the times."); for(i = 0; i < 10; i++) { printf("'%d' = %d ", i, cnt[i]); } return 0; }
4、
#include <stdio.h> int main(void) { int i, ch; int cnt[10] = {}; while((ch = getchar()) != EOF) { if(ch > '0' && ch < '9') { cnt[ch - '0']++; } } puts("show the times."); for(i = 0; i < 10; i++) { printf("'%d' = %d ", i, cnt[i]); } return 0; }