无法读取数字字符频率

Cant read number char frequency

#include <stdio.h>

int main() {
    FILE *fb;
    char data[255];
    int c=0;
    int count[75] = {0};
    fb = fopen("Input.txt", "r");
    fgets(data, 255, fb);

    /* Start finding frequency*/
    while (data[c] != '[=10=]')
    {
        if( data[c] >= '0' && data[c] <= 'z')
        count[data[c] - 'a']++;

        c++;
    }    
    for (c = 0; c < 75; c++)
    {
        /** Printing only those characters
            whose count is at least 1 */

        if (count[c] != 0)
            printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
    }

    return 0;
}

示例输入文件:"Fred Fish 12345678"

我能够从输入文件中处理 space,但程序无法读取大写字母和数字字符的频率。我可以在程序中更改什么来帮助解决问题。阅读频率后,我的计划是保存文件,以便我可以使用 Huffman

进行压缩

我想更改代码

count[data[c] - 'a']++;

count[data[c] - '0']++;

如果 data[c] 小于 75,则 count[data[c] - 'a'] 不起作用

#include <stdio.h>

int main() {
    FILE *fb;
    char data[255];
    int c = 0;
    int count[75] = { 0 };
    fb = fopen("Input.txt", "r");
    fgets(data, 255, fb);

    /* Start finding frequency*/
    while (data[c] != '[=10=]')
    {
        if (data[c] >= 'a' && data[c] <= 'z') // here you check normal letters
            count[data[c] - 'a']++;
        else if (data[c] >= 'A' && data[c] <= 'Z')
            count[data[c] - 'A' + 26]++; // Capital letters will be stored after lower cases
        else if (data[c] >= '0' && data[c] <= '9')
            count[data[c] - '0' + 51]++; // Numbers will be stored after capital letters
        c++;
    }

    // count[] is initialized as following : 
    // From count[0] to count[25] == Occurence of low case characters
    // From count[26] to count[51] == Occurence of capital characters
    // From count[52] to count[61] == Occurence of numbers from 0 to 9

    for (c = 0; c < 61; c++)
    {
        /** Printing only those characters
        whose count is at least 1 */
        if (count[c] != 0) {
            if (c < 26)
                printf("%c occurs %d times in the entered string.\n", c + 'a', count[c]); 

                // Starting from 'a', iterating until 'z'
            else if (c < 52)
                printf("%c occurs %d times in the entered string.\n", c + 'A' - 26, count[c]);

                // Same as for low case characters
                // Substracting 26 because we already have iterated through the first 26 low case characters
                // Starting from 'A' until 'Z'

            else if (c >= 52)
                printf("%c occurs %d times in the entered string.\n", c + '0' - 51, count[c]);

                // Same as for characters
                // Substracting 51 because we already have iterated through low cases and capital characters
                // Starting from '0' until '9'
        }
    }

    return 0;
}

问题是,考虑到 ascii table,您将大写字母和数字存储在数组的负索引中 count 通过减去等于 'a' =14=] 在 ASCII 中。这应该有效,但我还不能测试它,所以要小心。

对于打印,我们对小写字符、大写字符和数字执行相同的操作:我们从第一个开始:'a',然后是 'A',然后是 '0', 迭代直到最后一个使用 c 并打印它们。我们使用的事实是,在 C 中,'a' + 1 = 'b''A' + 1 = 'B'

对于输入 Fred Fish 12345678 此代码的输出是:

d occurs 1 times in the entered string.
e occurs 1 times in the entered string.
h occurs 1 times in the entered string.
i occurs 1 times in the entered string.
r occurs 1 times in the entered string.
s occurs 1 times in the entered string.
F occurs 2 times in the entered string.
1 occurs 1 times in the entered string.
2 occurs 1 times in the entered string.
3 occurs 1 times in the entered string.
4 occurs 1 times in the entered string.
5 occurs 1 times in the entered string.
6 occurs 1 times in the entered string.
7 occurs 1 times in the entered string.
8 occurs 1 times in the entered string.

您应该将代码中的 'a' 修改为 '0' 以考虑所有大写字母和数字。

#include <stdio.h>

int main() {

       ...
    /* snippet */
       ...    

    while (data[c] != '[=10=]')
    {   
        if( data[c] >= '0' && data[c] <= 'z')
        count[data[c] - '0']++;
        c++;                                                                                                                                                                                                 
    }   

    for (c = 0; c < 75; c++)
    {   
        /** Printing only those characters
         whose count is at least 1 */

        if (count[c] != 0)
            printf("%c occurs %d times in the entered string.\n",c+'0',count[c]);
    }   
    return 0;
}