统计数字,空格,换行和其他

// count digital whitespace and others
#include <stdio.h>

int main()
{
	// initalize the data
	int c, i, nwhite, nothers;
	int digital[10];
	nwhite = nothers = 0;
	for (i = 0; i <= 9; ++i)
	{
		digital[i] = 0;
	}

	// read and count
	while ((c = getchar()) != EOF)
	{
		if (c >= '0' && c <= '9')
			++digital[c - '0'];
		else if (c == ' ' || c == '\t' || c == '\n')
			++nwhite;
		else
			++nothers;
	}

	//output the result
	printf("digital = \n");
	for (i = 0; i <= 9; ++i)
		printf("the times of %d is %d\n", digital[i],digital[i]);
	printf("\n,nwhite is %d, nothers is %d", nwhite, nothers);
}


//学会读取字符,且能将其转化成数字

看的书上一步步做的,有以下几点新手需要注意的:

1,\n也算一个字符,按enter键也算。

2,for循环里用;隔开

3,数组的索引是从0到9的,int digital[10];

nwhite = nothers = 0;

for (i = 0; i

这里10应该改为9

4,getchar函数读取字符时要转换处理,读取的是字符

也是希望有大佬指导,等以后开始做比较大的项目时,就开始往GitHub上传。

作者我同时还在学python,感觉对python更感兴趣,python以它的简洁,高效深深吸引了我。

惯例附上张鼓励的图片

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值