C program: how to find the maximum/minimum frequency of a character in a string

后端 未结 1 975
情书的邮戳
情书的邮戳 2021-01-24 19:03

I am trying to find a way to get the values of max/min freq of characters in a string

E.g. helpme

would have max freq = 2 and min freq = 1 (there are 2 e\'s and

相关标签:
1条回答
  • 2021-01-24 19:43

    You need to build a histogram:

    size_t histogram[UCHAR_MAX] = { 0 };     // allocate and initialise histogram
    
    for (size_t i = 0; s[i] != '\0'; ++i)    // generate histogram for string s
    {
        histogram[(unsigned int)s[i]]++;
    }
    

    After this you can just do a linear scan through the histogram looking for the min and max letter frequencies.

    0 讨论(0)
提交回复
热议问题