c语言统计一个字符串的空格数,C语言:编写一个程序统计输入字符串中,各个数字、空白字符、以及其他所有字符出现的次数。...

#includeint main()

{

int c = 0;

int num_count = 0;

int emp_count = 0;

int els_count = 0;

while((c = getchar()) != EOF)

{

if((c >= '0')&&(c <= '9'))

{

num_count ++ ;

}

else if(c == ' ')

{

emp_count ++;

}

else

{

els_count ++;

}

}

printf("%d %d %d",num_count,emp_count,els_count);

return 0;

}

650) this.width=650;" style="width:604px;height:357px;" title="捕获.PNG 5.PNG" alt="wKiom1Yt2tSDLSaKAADOzp4evQI624.jpg" src="http://s3.51cto.com/wyfs02/M00/74/E8/wKiom1Yt2tSDLSaKAADOzp4evQI624.jpg" height="383" width="599" />

另外,方法2相比优与方法1

(1)可计算出每个数字具体有几次。

(2)对于空格使用了函数isspace()。

#includeint main()

{

int c = 0;

int num_count = 0;

int emp_count = 0;

int els_count = 0;

int arr[10] ={0};

int i = 0;

while((c = getchar()) != EOF)

{

if(c >= '0'&&c <= '9')

{

arr[c-'0'] ++;

}

else if(isspace(c))

{

emp_count ++;

}

else

{

els_count ++;

}

}

printf("emp_count: %d\n",emp_count);

printf("els_count: %d\n",els_count);

for( ; i<10;i++)

{

printf("%d:%d\n", i, arr[i]);

}

return 0;

}

650) this.width=650;" title="捕获.PNG3.PNG" src="http://s3.51cto.com/wyfs02/M01/75/03/wKioL1Ywlt7hETjCAADwVuvP04A502.jpg" alt="wKioL1Ywlt7hETjCAADwVuvP04A502.jpg" />

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值