C语言:统计一个文件中大写字符、小写字符、数字个数

统计data.txt文件中大写字符、小写字符、数字、其他字符的个数,代码如下:

/*统计一个文件中的大写字母  小写  字母  数字  及其他字符的个数*/
void chapter1::statistic()
{
	printf_s("统计文件开始\r\n");
	char *path = "D:\\000testprocess\\c_lesson\\Debug\\data.txt";
	FILE *f;
	int r = fopen_s(&f, path, "r");
	if (r>0)
	{
		printf_s("读取文件错误");
	}
	int capletter = 0, lowercase = 0 , num = 0, other = 0;
	char temp = fgetc(f);
	while (!feof(f))
	{
		if (temp>='a' && temp <='z')
		{
			lowercase++;
		}
		else if (temp >= 'A' && temp <= 'Z')
		{
			capletter++;
		}
		else if (temp >='0' && temp <= '9')
		{
			num++;
		}
		else
		{
			other++;
		}
		temp = fgetc(f);
	}
	printf_s("大写字符:%d  小写字符个数:%d  数字个数:%d   其他字符个数:%d",capletter,lowercase,num,other);

	fclose(f);
	printf_s("统计文件结束\r\n");
}

感兴趣的朋友可以关注下面的公众号,每天分享一点知识,成长看得见,感谢支持!!

 

  • 5
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值