#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);退出