C语言——实现字符分类统计

[复制链接]
发表于 2024-12-20 14:17:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. //字符分类统计函数
  2. #include<stdio.h>
  3. #include<string.h>
  4. void my_function(char ch[100])
  5. {
  6.         int i,words=0,digits=0,space=0,others=0;
  7.         for(i=0;i<strlen(ch);i++)
  8.                 {
  9.                         if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
  10.                                 {
  11.                                         words++;
  12.                                 }
  13.                         else if(ch[i]>='0'&&ch[i]<='9')
  14.                                 {
  15.                                         digits++;
  16.                                 }
  17.                         else if(ch[i]==' ')
  18.                                 {
  19.                                         space++;
  20.                                 }
  21.                         else
  22.                                 {
  23.                                         others++;
  24.                                 }
  25.                 }
  26.         printf("字母有 %d 个\n数字有 %d 个\n空格有 %d 个\n其他字符有 %d个\n",words,digits,space,others);
  27. }
  28. int main()
  29. {
  30.         char s[100];
  31.         int i,words=0,digits=0,space=0,others=0;
  32.         printf("请你输入一个字符串:");
  33.         gets(s);
  34.         my_function(s);
  35.         return 0;
  36. }
复制代码
运行结果如下:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5

GMT+8, 2025-7-19 06:27 , Processed in 0.230295 second(s), 32 queries 手机版|qidao123.com技术社区-IT企服评测▪应用市场 ( 浙ICP备20004199 )|网站地图

快速回复 返回顶部 返回列表