鱼C论坛

 找回密码
 立即注册
查看: 1844|回复: 2

[已解决]输入的数字、空白符和其他字符怎么分别储存在num[10],blank,others里并打印出来

[复制链接]
发表于 2021-11-19 23:40:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
编写一个用来统计输入的各个数字、空白符(空格、制表符、换行符)以及所有其他字符出现次数的程序。并分别存储在变量 num[10], blank , others 里边并打印出来。
最佳答案
2021-11-20 10:42:56
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         char s[256]                                                                   ;
  5.         int i , num[10] = {0} , blank = 0 , others = 0                                ;
  6.         for(i = 0 ; (s[i] = getchar()) != '\n' ; i ++) {
  7.                 if(s[i] >= '0' && s[i] <= '9') num[s[i] - '0'] ++                     ;
  8.                 else if(s[i] == ' ' || s[i] == '\t' || s[i] == '\n') blank ++         ;
  9.                 else others ++                                                        ;
  10.         }
  11.         for(i = 0 ; i < 10 ; i ++) if(num[i]) printf("%5c : %d\n" , '0' + i , num[i]) ;
  12.         printf(" blank : %d\n" , blank)                                               ;
  13.         printf("others : %d\n" , others)                                              ;
  14. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 123 + 256       * 8 / x + y
  4.     1 : 1
  5.     2 : 2
  6.     3 : 1
  7.     5 : 1
  8.     6 : 1
  9.     8 : 1
  10. blank : 14
  11. others : 6

  12. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-20 10:42:56 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         char s[256]                                                                   ;
  5.         int i , num[10] = {0} , blank = 0 , others = 0                                ;
  6.         for(i = 0 ; (s[i] = getchar()) != '\n' ; i ++) {
  7.                 if(s[i] >= '0' && s[i] <= '9') num[s[i] - '0'] ++                     ;
  8.                 else if(s[i] == ' ' || s[i] == '\t' || s[i] == '\n') blank ++         ;
  9.                 else others ++                                                        ;
  10.         }
  11.         for(i = 0 ; i < 10 ; i ++) if(num[i]) printf("%5c : %d\n" , '0' + i , num[i]) ;
  12.         printf(" blank : %d\n" , blank)                                               ;
  13.         printf("others : %d\n" , others)                                              ;
  14. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 123 + 256       * 8 / x + y
  4.     1 : 1
  5.     2 : 2
  6.     3 : 1
  7.     5 : 1
  8.     6 : 1
  9.     8 : 1
  10. blank : 14
  11. others : 6

  12. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-20 11:36:45 | 显示全部楼层
本帖最后由 jhq999 于 2021-11-20 11:39 编辑
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int num[10]={0},blank[3]={0},others=0,i=0,j=0;
  5.         char strch[256]={0};
  6.         while(1)
  7.         {
  8.                 scanf("%[^\n]",strch);
  9.                 fflush(stdin);
  10.                 if((strch[0]=='e'&&strch[1]=='x')&&(strch[2]=='i'&&strch[3]=='t'))break;
  11.                 i=0;
  12.                 while(strch[i])
  13.                 {
  14.                         switch(strch[i])
  15.                         {
  16.                         case '\t':
  17.                                 blank[1]++;
  18.                                 break;
  19.                         case ' ':
  20.                                 blank[2]++;
  21.                                 break;
  22.                         default:
  23.                                 if('0'<=strch[i]&&'9'>=strch[i])
  24.                                         num[strch[i]-'0']++;
  25.                                 else
  26.                                         others++;
  27.                                 break;

  28.                         }
  29.                         i++;
  30.                 }

  31.                 blank[0]++;

  32.         }
  33.         printf("\\n is:%d,\\t is: %d,space is: %d\n",blank[0],blank[1],blank[2]);
  34.         for ( i = 0; i < 10; i++)
  35.         {
  36.                 printf("%d is:%d\n",i,num[i]);
  37.         }
  38.         printf("others is:%d",others);
  39.         return 0;
  40. }         
复制代码
  1. gh67t67t67t                     9h98jiojj    oko0kko
  2. jiojpu787867564vbbh uhu7689098967809
  3. 6876y86
  4. 8090jhygty6t
  5. exit
  6. \n is:4,\t is: 3,space is: 6
  7. 0 is:5
  8. 1 is:0
  9. 2 is:0
  10. 3 is:0
  11. 4 is:1
  12. 5 is:1
  13. 6 is:11
  14. 7 is:9
  15. 8 is:9
  16. 9 is:7
  17. others is:38

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 13:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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