【发布时间】:2021-01-14 22:05:17
【问题描述】:
这是我的职责。我的主要重点是获取字符频率和最高字符频率。
下面的函数(get_letter_frequencies)应该是获取一个字符串示例(“I am a big boy”)并返回字符频率和最高字符频率。
函数应该返回
我 - 2
一 - 2
米 - 1
b - 2
g - 1
o - 1
y - 1
最高字符频率将是“iab”
我的问题在于 get_letter_frequencies 函数。为了返回上述输出,我应该从函数中安排什么?
void get_letter_frequencies(const char *text, size_t len, int freq[26], int *max_freq)
{
for(int i = 0; i<len; i++)
{
if(text[i] != ' ' || !(is_sentence_terminator(text[i]))) //this condition is set in order to ignore the spaces and the sentence terminators (! ? .)
{
if(text[i] >= 'a' && text[i] <= 'z')
{
freq[text[i] - 'a']++;
}
}
}
for(int j = 0; j < 26; j++)
{
if(freq[j] >= 1)
{
*max_freq = freq[j];
}
}
下面这个函数(is_sentence_terminator)。在这里,该函数检查句子是否以“!?或。”结尾,如果它没有以一个终止符结尾,那么它不是一个句子并忽略它。
int is_sentence_terminator(char ch)
{
if(ch == 33 || ch == 46 || ch == 63)
{
return 1;
}else
{
return 0;
}
}
【问题讨论】:
-
一方面,您的
max_freq循环不应该检查先前的最大值而不是1吗? -
@500-InternalServerError 您能否解释一下,因为我不明白您的意思。谢谢
-
@chqrlie:我安排它是为了消除大喊大叫的效果 :) 再说一遍,我不是故意的。
-
@AnthonyMifsud:见 Chqrlie 的回答。