c鞋敲小白 2021-12-07 16:14 采纳率: 85.7%
浏览 15

统计不同字符如何简化

Description

输入一行字符,分别统计其中英文字母、空格、数字和其它字符的个数。

Input

键盘上的任意字符串,以;结束。

Output

character= ,blank= ,digit= ,other= ;

Sample Input

case 1:printf("very good!");
Sample Output

character=18,blank=2,digit=1,other=6;

  • 写回答

3条回答 默认 最新

  • c鞋敲小白 2021-12-07 16:18
    关注

    我这个答案是正确的 但运行超时 我不知道如何简化
    #include <stdio.h>
    int main()
    { char str[100];
    int character=0,blank=0,digit=0,other=0,i=0;
    printf("",str);
    while((str[i]=getchar())!='\n')
    {
    if (str[i]>='a'&&str[i]<='z'||str[i] >='A'&&str[i]<='Z')
    {
    character++;
    }
    else if(str[i]==' ')
    {
    blank++;
    }
    else if (str[i]>='0'&&str[i]<='9')
    {
    digit++;
    }
    else
    {
    other++;
    }
    }
    printf("character=%d,blank=%d,digit=%d,other=%d;",character,blank,digit,other);
    return 0;
    }

    评论

报告相同问题?

问题事件

  • 创建了问题 12月7日

悬赏问题

  • ¥15 matlab基于鲸鱼算法优化vmd
  • ¥20 PCB设计与制作问答
  • ¥20 课堂跑如何下载ppt
  • ¥15 有没有哥看着画下电路
  • ¥15 STM32阿里云平台显示不了传感器采集的数据
  • ¥20 三菱FX系列PLC上位机串口下指令置位M64和M65,这两条指令分别是什么呢?
  • ¥15 有关结冰模拟程序咨询
  • ¥15 ubuntu服务器配置dns域名后无法访问其他域名
  • ¥50 本人复制了一个关于股票指标的代码,但是运行失败,有没有人帮我解决一下
  • ¥50 用matlab和numeca做透平机械流体力学和热力学模拟 价格可议