我正在尝试按字符串中的频率对字母进行排序。如果两个或多个字母具有相同的频率,则具有相同频率的字母将按字母顺序排序。
这就是我到目前为止所取得的
void get_text_statistics(const char *text, size_t len, statistics_t *data)
{
*data = (statistics_t)
{
.sentences = count_sentences(text, len),
.words = count_words(text, len),
.most_freq_chars = {/*something needs to be here*/}
}
get_letter_frequencies(text, len, &data -> freq[0], &data -> max_freq)
}
如您所见,我的问题是尝试按频率对字符串中的字母进行排序。我尝试查找一些教程,但找不到与此特定示例类似的内容。这是有关的结构。
typedef struct statistics
{
char_counts_t char_info;
int sentences;
int words;
int freq[26];
int max_freq;
char most_freq_chars[27];
} statistics_t;
早些时候,我设法制作了这个可能会有所帮助的功能。
void get_letter_frequencies(const char *text, size_t len, int freq[26], int *max_freq)
{
for (int i = 0; i < 26; i++)
freq[i] = 0;
for (int i = 0; i < len; i++) {
if ((text[i] >= 97) && (text[i] <= 122))
freq[text[i] - 97]++;
*max_freq = 0;
for (int i = 0; i < 26; i++)
if (*max_freq < freq[i])
*max_freq = freq[i];
}
我该怎么办?TIA
ps: count_sentences
andcount_words
是对字符串中的句子和单词进行计数的函数。