DigitCounting-统计字符个数

对数字、空格和其他字符进行计数。此处的空白包括“\n”、“和”\t”。注意,在输入样本的末尾有一个不可见的“\n”。

count digits, white space, and other characters. White space here includes '\n', ' ' and '\t'. Noting that there is an unvisible '\n' at the end of input sample.

输入格式:

text

输出格式:

digits = x x x x x x x x x x, white space = x, other = x

输入样例:

12 a 3

结尾无空行

输出样例:

digits = 0 1 1 1 0 0 0 0 0 0, white space = 3, other = 1

结尾无空行

常见的一道题,细节比较多,比如输出的格式,很容易少空格之类的。

#include<stdio.h>
int main(void){
    char c=0;
    int a[10]={0};
    int i,w=0,o=0;
    while((c=getchar())!=EOF){
      if(c>='0'&&c<='9'){
      a[c-'0']++;//这里要注意c-'0',直接打c是不行的
      }
        else if(c==' '||c=='\n'||c=='\t')
            w++;
          else o++;
    }
    printf("digits = ");
    for(i=0;i<=9;i++){
        if(i==9)
            printf("%d",a[i]);
      else  printf("%d ",a[i]);
    }
    printf(", white space = %d",w);
    printf(", other = %d",o);
    
    return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值