#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char string[81];
int bigLetter = 0, smallLetter = 0, space = 0, number = 0, elseChar = 0, i;
fp = fopen("essay.txt", "r");
if (fp == NULL) exit(0);
while (fgets(string,81,fp) != NULL)
{
for (i = 0; i < 81; i++)
{
if (string[i] >= 'A' && string[i] <= 'Z')
bigLetter++;
else if (string[i] >= 'a' && string[i] <= 'Z')
smallLetter++;
else if (string[i] == ' ')
space++;
else if (string[i] >= '0' && string[i] <= '9')
number++;
else
elseChar++;
}
}
printf("大写字母%d个,小写字母%d个,空格%d个,数字%d个,其他%d个\n", bigLetter, smallLetter, space, number, elseChar);
fclose(fp);
system("pause");
return 0;
}
错的地方有:
1:用exit(0);需包含
#include "stdlib.h"
2:对小写字母统计条件语句里,后面大写Z应改为小写z
else if (string[i] >= 'a' && string[i] <= 'Z')
//to
else if (string[i] >= 'a' && string[i] <= 'z')
3:由于是大小写及数字空格统计
请确定存在这个essay.txt文件
不然将顺着exit(0);退出