2

I'm trying to get some statistics from a string of text such as the number of words and sentences, etc. Among those statistics is the most frequent letters used in the string. I'm currently using these functions below to get most of the statistics required.

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];
        }
    }
}


void get_text_statistics(const char *text, size_t len, statistics_t *data)
{

    data->words = count_words(text, len);
    data->sentences = count_sentences(text, len);

    get_char_counts(text, len, &data->char_info);
    get_letter_frequencies(text, len, data->freq, &data->max_freq);


    for (int q = 0; q < 26; q++)
        data->most_freq_chars[q] = 0;

    for (int q = 0; q < 26; q++)
    {
        int max = 0;
        if (data->max_freq == data->freq[q])
        {
            data->most_freq_chars[max] = (char) (q + 97);
            max++;
        }
    }
}

Now when trying to tabulate the statistics, I used a while loop just in case there are two or more letters which have the same frequency.

printf("| Most frequent letter(s)        |  [%c]  |\n", data.most_freq_chars[0]);

int a = 1;
while (data.most_freq_chars[a] != '\0')
{
printf("|                                |  [%c]   |\n", data.most_freq_chars[a]);
a++;
}

To try out this scenario, I inputted the string "Hello. Goodluck!" with the expectation of getting two most frequent letters([o] and [l]), however, only one of them comes up ([o]).

How do I go about this?

Thanks in advance

gann
  • 49
  • 9

1 Answers1

0

The code to identify max_freq should be outside of the loop which calculates the frequencies of different letters, since we can calculate max frequency only after all the frequencies get known.

Thus, theget_letter_frequencies function should be written like this -

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];
}
Suraj Upadhyay
  • 475
  • 3
  • 9