[C++] C语言 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 →→→→→进入此内容的聊天室

来自 Hot Iguana, 2024-03-21, 写在 C++, 查看 12 次.
URL http://www.code666.cn/view/528f6a47
  1. 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
  2.  
  3. 程序分析:利用while语句,条件为输入的字符不为'\n'
  4.  
  5.  
  6. #include<stdio.h>
  7. int main()
  8. {
  9.     char c;
  10.     int letters=0,spaces=0,digits=0,others=0;
  11.     printf("请输入一些字母:\n");
  12.     while((c=getchar())!='\n')
  13.     {
  14.         if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
  15.             letters++;
  16.         else if(c>='0'&&c<='9')
  17.             digits++;
  18.         else if(c==' ')
  19.             spaces++;
  20.         else
  21.             others++;
  22.     }
  23.     printf("字母=%d,数字=%d,空格=%d,其他=%d\n",letters,digits,spaces,others);
  24.     return 0;
  25. }

回复 "C语言 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。"

这儿你可以回复上面这条便签

captcha