• c语言中数字字符计数


    c语言中数字字符计数

    1、

    #include <stdio.h>
    
    int main(void)
    {
        int i, ch;
        int cnt[10] = {};
        
        while((ch = getchar()) != EOF)
        {
            switch(ch)
            {
                case '0': cnt[0]++; break;
                case '1': cnt[1]++; break;
                case '2': cnt[2]++; break;
                case '3': cnt[3]++; break;
                case '4': cnt[4]++; break;
                case '5': cnt[5]++; break;
                case '6': cnt[6]++; break;
                case '7': cnt[7]++; break;
                case '8': cnt[8]++; break;
                case '9': cnt[9]++; break;
            }
        }
        
        puts("show the times.");
        for(i = 0; i < 10; i++)
        {
            printf("'%d' = %d
    ", i, cnt[i]);
        }
        
        return 0;
    }

     

    2、

    #include <stdio.h>
    
    int main(void)
    {
        int i, ch;
        int cnt[10] = {};
        
        while((ch = getchar()) != EOF)
        {
            switch(ch)
            {
                case 48: cnt[0]++; break;
                case 49: cnt[1]++; break;
                case 50: cnt[2]++; break;
                case 51: cnt[3]++; break;
                case 52: cnt[4]++; break;
                case 53: cnt[5]++; break;
                case 54: cnt[6]++; break;
                case 55: cnt[7]++; break;
                case 56: cnt[8]++; break;
                case 57: cnt[9]++; break;
            }
        }
        
        puts("show the times.");
        for(i = 0; i < 10; i++)
        {
            printf("'%d' = %d
    ", i, cnt[i]);
        }
        
        return 0;
    }

    3、

    #include <stdio.h>
    
    int main(void)
    {
        int i, ch;
        int cnt[10] = {};
        
        while((ch = getchar()) != EOF)
        {
            if(ch >= 48 && ch <= 57)
            {
                cnt[ch - 48]++;
            }
        }
        
        puts("show the times.");
        for(i = 0; i < 10; i++)
        {
            printf("'%d' = %d
    ", i, cnt[i]);
        }
        
        return 0;
    }

    4、

    #include <stdio.h>
    
    int main(void)
    {
        int i, ch;
        int cnt[10] = {};
        
        while((ch = getchar()) != EOF)
        {
            if(ch > '0' && ch < '9')
            {
                cnt[ch - '0']++;
            }
        }
        
        puts("show the times.");
        for(i = 0; i < 10; i++)
        {
            printf("'%d' = %d
    ", i, cnt[i]); 
        }
        
        return 0;
    }

  • 相关阅读:
    用jmeter通过ssl验证访问https
    github jekyll主题
    JMeter中返回Json数据的处理方法
    使用Nginx实现反向代理
    Antd 表格 -- 自定义合并行处理
    Go语言--第8章 包(package)
    GO语言 -- 第7章 接口(INTEFACE)
    GO语言--第6章 结构体
    PHP--相关扩展安装
    Go语言--第5章 函数
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14803469.html
Copyright © 2020-2023  润新知