我正在尝试编写一个程序,它接受输入文本文件并找到最常见的字符和次数。我的结果一直是0。这是我的代码:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
char *filename;
char ch;
int array[255] = {0};
char str[]= "";
int i, max, index;
// Check if a filename has been specified in the command
if (argc < 2)
{
printf("Missing Filename\n");
return(1);
}
else
{
filename = argv[1];
printf("Filename : %s\n", filename);
}
// Open file in read-only mode
fp = fopen(filename,"r");
// If file opened successfully, then print the contents
if ( fp )
{
printf("File contents:\n");
while ( (ch = fgetc(fp)) != EOF )
{
for (i = 0; i < 255; i++){
ch = str[i];
}
// Find the letter that was used the most
for(i = 0; str[i] != 0; i++)
{
++array[str[i]];
}
max = array[0];
index = 0;
for(i = 0; str[i] != 0; i++)
{
if( array[str[i]] > max)
{
max = array[str[i]];
index = i;
}
}
printf("The max character is: %c \n", str[index]);
printf("The amount is %d\n", max);
}
}
else
{
printf("Failed to open the file\n");
}
return(0);
}
这是代码的输出:
文件名:text.txt
文件内容:
最大字符是:
金额为0
最大字符是:
金额为0
最大字符是:
金额为0
最大字符是:
金额为0
答案 0 :(得分:1)
int array[255]
太小了。 unsigned char
的范围是0到255,包括255,因此它应该是array[256]
您只需计算每个字符的出现次数:
while((ch = fgetc(fp)) != EOF)
array[ch]++;
请注意,ch
应声明为int
,而不是char
,其范围包含负数。将其声明为char
会在读取非ASCII文件时导致错误。您可以稍后将ch
投回(char)
。
然后找出最常见的字符:
int most_common_char = 0;
for(i = 1; i < 256; i++)
if(array[i] > array[most_common_char])
most_common_char = i;
我建议使用硬编码文件名进行测试,例如const char *filename = "test.txt"
,这样可以更容易地调试代码。
示例:
int main()
{
const char *filename = "test.txt";
FILE *fp;
int ch;
int array[256] = { 0 };
int i;
int most_common_char;
fp = fopen(filename, "r");
if(!fp)
{
printf("Failed to open the file\n");
return 0;
}
while((ch = fgetc(fp)) != EOF)
array[ch]++;
most_common_char = 0;
for(i = 1; i < 256; i++)
if(array[i] > array[most_common_char])
most_common_char = i;
printf("most common character: %c\n", (char)most_common_char);
return(0);
}