LYy0 2022-11-10 17:33 采纳率: 61.5%
浏览 15
已结题

C语言字母个数计算错误

img


想问一下,我输入其他字符都不会少算就是字母一定会少算一个,到底是哪里出了问题

  • 写回答

1条回答 默认 最新

  • 心寒丶 优质创作者: 编程框架技术领域 2022-11-10 18:00
    关注

    上边scanf去掉,可以参考下这个

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        char c;
        int letters=0;
        int space=0;
        int digit=0;
        int other=0;
        printf ("请输入一行字符:>");
         while ((c=getchar())!='\n')
        {
            if ((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z'))
            {
                letters++;
            }
            else if (' ' == c)
            {
                space++;
            }
            else if (c >= '0' && c <= '9')
            {
                digit++;
            }
            else
            {
                other++;
            }
        }
        printf ("字母的个数:>%d\n空格的个数:>%d\
                \n数字的个数:>%d\n其他字符的个数:>%d\n",\
            letters,space,digit,other);
        system ("pause");
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 11月19日
  • 已采纳回答 11月11日
  • 创建了问题 11月10日