统计各种字符的出现次数

#include <stdio.h>
//统计各个数字、空白符(包括空格符、制表符、换行符)、以及其他所有字符的出现次数
int main(void){
	int c;
	int digit[10]={0};
	int blank=0;
	int other=0;

	while((c=getchar())!=EOF){
		if(c==' '||c=='\t'||c=='\n')
			blank++;
		else if(c>='0'&&c<='9')
			digit[c-'0']++;//可以充当数组的合法下标
		else
			other++;
	}
	printf("空白符:%d\n",blank);
	for(int i=0;i<10;i++)
		printf("%d:%d\n",i,digit[i]);
	printf("其他字符:%d\n",other);
}

猜你喜欢

转载自blog.csdn.net/weixin_38911591/article/details/89679867