qq_43412960 2019-10-22 23:15 采纳率: 78.6%
浏览 221
已采纳

这是一个关于C语言getcahr()的问题

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()

{ char c='\0';

int digit=0,block=0,others=0,letters=0;
printf("请输入长度小于100的字符");
while(c=getchar()!='\n')
{if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
letters++;
  else if((c-'0')>=0&&(c-'0')<=9)
      digit++;
  else if(c==' ')
      block++;
  else
      others++;

}

printf("%d%d%d%d",letters,digit,block,others);
system("pause");


return 0;
}

这是我写的程序。功能是输入字符串,然后识别,我运行后没有得到合适结果。于是调试,发现,不管输入什么值,c的内容都是方框,如下图所示:
图片说明
请大佬们帮忙解决

  • 写回答

5条回答 默认 最新

  • Peter(阿斯拉达) 博客专家认证 2019-10-23 09:25
    关注
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    int main()
    { 
        char c='\0';
    
        int digit=0,block=0,others=0,letters=0;
        printf("请输入长度小于100的字符\n");
        while((c=getchar())!='\n')
        {
            if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
                letters++;
            else if((c-'0')>=0&&(c-'0')<=9)
                digit++;
            else if(c==' ')
                block++;
            else
                others++;
        }
    
        printf("%d:%d:%d:%d\n",letters,digit,block,others);
        system("pause");
    
        return 0;
    }
    

    输出:

    请输入长度小于100的字符
    asdfa asd 433
    8:3:3:0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置