【发布时间】:2015-04-25 17:25:55
【问题描述】:
问题陈述:一个C程序来计算一个字符在文件中出现的次数。字符被认为不区分大小写。 我已将文件中的输入字符和字符都转换为大写字母,这样所有字符的出现都不会被计算在内。但是当我在在线编辑器上执行此操作时,得到的结果是“错误答案”,编辑器不接受此代码。这段代码有什么错误??
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main
{
FILE *fp;
char filename[20];
char character;
char compare;
int to_upper1;
int to_upper2;
int count=0;
printf("\nEnter the file name");
scanf("%s", filename);
fp = fopen(filename,"r");
if(fp == NULL)
{
exit(-1);
}
printf("\nEnter the character to be counted");
scanf("%c", &character);
to_upper1 = toupper(character);
while((compare = fgets(fp)) != EOF)
{
to_upper2 = toupper(compare);
if(to_upper1 == to_upper2)
count++;
}
printf("\nFile \'%s\' has %d instances of letter \'%c\'", filename, count, character);
return 0;
}
【问题讨论】:
-
你编译过这个吗?测试了吗?
-
sed s/fgets/fgetc/.
标签: c file-handling case-insensitive