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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章