打印字符串的字母频率

Printing letter frequency of a string

只是想知道为什么这不起作用。我想使用 getchar() 读取输入并打印字符、percentage/100 然后是频率。例如你好将打印: 'a' 0.200000 1 依此类推到 z 的每个字符。 它接受输入但不打印任何内容:

#include <stdio.h>
int main (void) {
    double array[26] = {0};
    int i=0;
    double count=0;
    int j =0;
    int start = 'a';
    int point = 0;
    while (i != EOF) {
            i=getchar();
            if(i>='a' && i<='z' && i!=-1) {
                point= i-'a';
                array[point] = array[point] +1;
                count= count+1;
            }
            else if(i>='A' && i<='Z' && i!=-1) {
                point = i - 'A';
                array[point] = array[point] +1;
                count= count+1;
            }
    }
    while (j<=25) {
        printf("'%c' %.6f %.0f", start, (count/array[j]), array[j]);
        j++;
        start++;
    }
}

你计算百分比的方式是错误的。

printf("'%c' %.6f %.0f", start, (count/array[j]), array[j]);

应该是

printf("'%c' %.6f%% %.0f\n", start, (array[j]/count*100), array[j]);