fork(2) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. void count(char str[]) {
  4. int letter = 0, digit = 0, space = 0, other = 0;
  5. for (int i = 0; i < strlen(str); i++) {
  6. if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
  7. letter++;
  8. } else if (str[i] >= '0' && str[i] <= '9') {
  9. digit++;
  10. } else if (str[i] == ' ') {
  11. space++;
  12. } else {
  13. other++;
  14. }
  15. }
  16. printf("字母个数: %d\n数字个数: %d\n空格个数: %d\n其他字符个数: %d\n", letter, digit, space, other);
  17. }
  18. int main() {
  19. char str[100];
  20. fgets(str, sizeof(str), stdin);
  21. if (str[strlen(str) - 1] == '\n') {
  22. str[strlen(str) - 1] = '\0';
  23. }
  24. count(str);
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
字母个数: 0
数字个数: 0
空格个数: 0
其他字符个数: 0