在C中查找文件中最常见的字符

cad*_*ddy 3 c

我正在编写一个函数,用于查找文件中最常见的字母字符.该函数应忽略除字母以外的所有字符.

目前我有以下内容:

int most_common(const char *filename)
{
char frequency[26];
int ch = 0;

FILE *fileHandle;
if((fileHandle = fopen(filename, "r")) == NULL){
    return -1;
}

for (ch = 0; ch < 26; ch++)
    frequency[ch] = 0;

while(1){
    ch = fgetc(fileHandle);
    if (ch == EOF) break;

    if ('a' <= ch && ch  <= 'z')
        frequency[ch - 'a']++;
    else if ('A' <= ch && ch <= 'Z')
        frequency[ch - 'A']++;
}

int max = 0;
for (int i = 1; i < 26; ++i)
  if (frequency[i] > frequency[max])
      max = i;

return max;
}
Run Code Online (Sandbox Code Playgroud)

现在函数返回最常出现的字母发生的次数,而不是字符本身.我有点迷失,因为我不确定这个功能应该是什么样子.它是否有意义,我怎么可能解决这个问题?

我将衷心感谢您的帮助.

Joe*_*Joe 5

变量frequency由字符代码索引.所以frequency[0]是5,如果有5'一个.

在您的代码中,您要分配计数max,而不是字符代码,因此您将返回计数而不是实际字符.

您需要存储最大频率计数和它所引用的字符代码.

我会解决这个问题:

int maxCount = 0;
int maxChar = 0;
// i = A to Z
for (int i = 0; i <= 26; ++i)
{
  // if freq of this char is greater than the previous max freq
  if (frequency[i] > maxCount)
  {
      // store the value of the max freq
      maxCount = frequency[i];

      // store the char that had the max freq
      maxChar = i;
  }
}

// character codes are zero-based alphabet.
// Add ASCII value of 'A' to turn back into a char code.
return maxChar + 'A';
Run Code Online (Sandbox Code Playgroud)

请注意,我换int i = 1int i = 0.从1开始意味着开始B,这是一个你可能不会注意到的微妙错误.此外,循环应该终止<= 26,否则你也会错过Z.

注意大括号.您的大括号样式(单语句块没有大括号)非常不推荐.

此外,i++++i这种情况更常见.在这种情况下它将没有任何区别,所以建议i++.