fork download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. void count_chars(const char *s, int *letter, int *digit, int *space, int *other) {
  5. *letter = 0;
  6. *digit = 0;
  7. *space = 0;
  8. *other = 0;
  9. while (*s != '\0') {
  10. if (isalpha(*s)) {
  11. (*letter)++;
  12. } else if (isdigit(*s)) {
  13. (*digit)++;
  14. } else if (isspace(*s)) {
  15. (*space)++;
  16. } else {
  17. (*other)++;
  18. }
  19. s++;
  20. }
  21. }
  22.  
  23. int main() {
  24. char s[100];
  25. printf("请输入字符串:");
  26. getchar();
  27. gets(s);
  28.  
  29. int letter, digit, space, other;
  30. count_chars(s, &letter, &digit, &space, &other);
  31.  
  32. printf("字母个数:%d\n", letter);
  33. printf("数字个数:%d\n", digit);
  34. printf("空格个数:%d\n", space);
  35. printf("其他字符个数:%d\n", other);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 5288KB
stdin
vi58fh
stdout
请输入字符串:字母个数:3
数字个数:2
空格个数:0
其他字符个数:0