天天看点

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");
}
           

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

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

继续阅读