I am trying to sort letters by frequency in a string. In the case that two or more letters have the same frequency, the letters with the same frequency would be sorted in alphabetical order.
This is what I managed to get so far
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)
}
As you can see, my problem here is trying to sort the letters in a string by freq. I've tried looking up some tutorials but I couldn't find something similar to this specific example. Here is the struct concerned.
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;
A bit earlier, I managed to make this function which may help.
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];
}
How would I go about this?
TIA
p.s: count_sentences
and count_words
are functions which count sentences and words in the string.
question from:
https://stackoverflow.com/questions/65875212/sorting-letters-in-an-array-by-frequency-in-within-a-struct 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…