summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/freq_analysis/freq_analysis.c
blob: c60d7a6bde0fb815af335acca8b2e8f7e05a907d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>

int find_rank(int h[26], int min)
{
    int res = 0;
    for (int i = 0; i < 26; i++)
    {
        res += h[i] > h[min] || (h[i] == h[min] && i < min);
    }
    return res;
}

void freq_analysis(const char text[], const char table[])
{
    int h[26] = { 0 };
    for (int i = 0; text[i]; i++)
    {
        h[text[i] - 'A']++;
    }

    for (int j = 0; j < 26; j++)
    {
        if (h[j] != 0)
        {
            printf("%c %c\n", j + 'A', table[find_rank(h, j)]);
        }
    }
}