鱼C论坛

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

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

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

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

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

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

int main(void)
{
        char s[256]                                                                   ;
        int i , num[10] = {0} , blank = 0 , others = 0                                ;
        for(i = 0 ; (s[i] = getchar()) != '\n' ; i ++) {
                if(s[i] >= '0' && s[i] <= '9') num[s[i] - '0'] ++                     ;
                else if(s[i] == ' ' || s[i] == '\t' || s[i] == '\n') blank ++         ;
                else others ++                                                        ;
        }
        for(i = 0 ; i < 10 ; i ++) if(num[i]) printf("%5c : %d\n" , '0' + i , num[i]) ;
        printf(" blank : %d\n" , blank)                                               ;
        printf("others : %d\n" , others)                                              ;
}
        编译、运行实况:
D:\00.Excise\C>g++ -o x x.c

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

D:\00.Excise\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

int main(void)
{
        char s[256]                                                                   ;
        int i , num[10] = {0} , blank = 0 , others = 0                                ;
        for(i = 0 ; (s[i] = getchar()) != '\n' ; i ++) {
                if(s[i] >= '0' && s[i] <= '9') num[s[i] - '0'] ++                     ;
                else if(s[i] == ' ' || s[i] == '\t' || s[i] == '\n') blank ++         ;
                else others ++                                                        ;
        }
        for(i = 0 ; i < 10 ; i ++) if(num[i]) printf("%5c : %d\n" , '0' + i , num[i]) ;
        printf(" blank : %d\n" , blank)                                               ;
        printf("others : %d\n" , others)                                              ;
}
        编译、运行实况:
D:\00.Excise\C>g++ -o x x.c

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

D:\00.Excise\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

int main(void)
{
        int num[10]={0},blank[3]={0},others=0,i=0,j=0;
        char strch[256]={0};
        while(1)
        {
                scanf("%[^\n]",strch);
                fflush(stdin);
                if((strch[0]=='e'&&strch[1]=='x')&&(strch[2]=='i'&&strch[3]=='t'))break;
                i=0;
                while(strch[i])
                {
                        switch(strch[i])
                        {
                        case '\t':
                                blank[1]++;
                                break;
                        case ' ':
                                blank[2]++;
                                break;
                        default:
                                if('0'<=strch[i]&&'9'>=strch[i])
                                        num[strch[i]-'0']++;
                                else
                                        others++;
                                break;

                        }
                        i++;
                }

                blank[0]++;

        }
        printf("\\n is:%d,\\t is: %d,space is: %d\n",blank[0],blank[1],blank[2]);
        for ( i = 0; i < 10; i++)
        {
                printf("%d is:%d\n",i,num[i]);
        }
        printf("others is:%d",others);
        return 0;
}         
gh67t67t67t                     9h98jiojj    oko0kko
jiojpu787867564vbbh uhu7689098967809
6876y86
8090jhygty6t
exit
\n is:4,\t is: 3,space is: 6
0 is:5
1 is:0
2 is:0
3 is:0
4 is:1
5 is:1
6 is:11
7 is:9
8 is:9
9 is:7
others is:38
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-23 01:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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