题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
#include <stdio.h> #include <string.h> int main() { char a[1001]= {0}; int i,len; int cnt[4] = {0}; fgets(a, 1000, stdin); len = strlen(a); for (i = 0; i < len; i++) { if (((a[i] >= 'A') && (a[i] <= 'Z')) || ((a[i] >= 'a') && (a[i] <= 'z'))) { cnt[0]++; }else if(a[i] == ' ') { cnt[1]++; } else if ((a[i] >= '0') && (a[i] <= '9')){ cnt[2]++; } else { /* fgets 会把换行符存下来 */ if (a[i] != '\n') { cnt[3]++; } } } for (i = 0; i < 4; i++) { printf("%d\n",cnt[i]); } return 0; }