m0_53936501 2021-05-10 00:21 采纳率: 100%
浏览 39
已采纳

有关c++的一个问题不知哪里出错了求教?

本人学生课本有一道题

请编写程序,实现统计字符串s中数字字符‘0’~‘9’出现的次数,并依次存储在长度为10的数组count中。

以下是本人编写的程序,请指教。

 

#include <stdio.h>
void main()
{
    char c;
    int count[10] = {0,0,0,0,0,0,0,0,0,0};
    printf("请输入任意字符(输入*终止)\n");
    while ((c = getchar()) != '*');
    {
        if (c == 0)
            count[0]++;
        else if (c == 1)
            count[1]++;
        else if (c == 2)
            count[2]++;
        else if (c == 3)
            count[3]++;
        else if (c == 4)
            count[4]++;
        else if (c == 5)
            count[5]++;
        else if (c == 6)
            count[6]++;
        else if (c == 7)
            count[7]++;
        else if (c == 8)
            count[8]++;
        else if (c == 9)
            count[9]++;
        else;
    }
    printf("%d %d %d %d %d %d %d %d %d %d", count[0], count[1], count[2], count[3], count[4], count[5], count[6], count[7], count[8], count[9]);
}
 

正常应该是输入55566677789输出0000033311,可是每次不够输入什么输出都为0000000000

 

  • 写回答

6条回答 默认 最新

  • 关注
    #include <stdio.h>
    void main()
    {
        char c;
        int count[10] = {0,0,0,0,0,0,0,0,0,0};
        printf("请输入任意字符(输入*终止)\n");
        while ((c = getchar()) != '*')
        {
            if (c == '0')
                count[0]++;
            else if (c == '1')
                count[1]++;
            else if (c == '2')
                count[2]++;
            else if (c == '3')
                count[3]++;
            else if (c == '4')
                count[4]++;
            else if (c == '5')
                count[5]++;
            else if (c == '6')
                count[6]++;
            else if (c == '7')
                count[7]++;
            else if (c == '8')
                count[8]++;
            else if (c == '9')
                count[9]++;
            else;
        }
        printf("%d %d %d %d %d %d %d %d %d %d", count[0], count[1], count[2], count[3], count[4], count[5], count[6], count[7], count[8], count[9]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助