fork download
  1. #include <stdio.h>
  2. #include <stdlib.h> // exit()
  3. #include <ctype.h> // isgraph()
  4.  
  5. void readfreq(FILE *file, int *table) {
  6. int c = fgetc(file);
  7.  
  8. for (; c != EOF; c = fgetc(file))
  9. table[c]++;
  10. }
  11.  
  12. int main(int argc, char **argv) {
  13. int table[256] = { 0 };
  14. FILE *file = argc == 2 ? fopen(argv[1], "r") : stdin;
  15.  
  16. if (!file) perror("Error"), exit(1);
  17.  
  18. readfreq(file, table);
  19.  
  20. for (int i = 0; i < 256; i++)
  21. if (table[i] > 0)
  22. printf(isgraph(i) ? "'%c' => %d\n"
  23. : "0x%02x => %d\n",
  24. i, table[i]);
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 2012KB
stdin
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void readfreq(FILE *file, int *table) {
    int c = fgetc(file);

    for (; c != EOF; c = fgetc(file))
        table[c]++;
}

int main(int argc, char **argv) {
    int table[256] = { 0 };
    FILE *file = argc == 2 ? fopen(argv[1], "r") : stdin;

    if (!file) perror("Error"), exit(1);

    readfreq(file, table);

    for (int i = 0; i < 256; i++)
        if (table[i] > 0)
            printf(isgraph(i) ? "'%c'  => %d\n"
                              : "0x%02x => %d\n",
                   i, table[i]);

    return 0;
}
stdout
0x0a => 27
0x20 => 172
'!'  => 2
'"'  => 8
'#'  => 3
'%'  => 4
'''  => 2
'('  => 14
')'  => 14
'*'  => 5
'+'  => 4
','  => 7
'.'  => 3
'0'  => 6
'1'  => 2
'2'  => 4
'5'  => 2
'6'  => 2
':'  => 2
';'  => 12
'<'  => 4
'='  => 10
'>'  => 6
'?'  => 2
'E'  => 4
'F'  => 3
'I'  => 2
'L'  => 2
'O'  => 1
'['  => 5
'\'  => 2
']'  => 5
'a'  => 15
'b'  => 7
'c'  => 14
'd'  => 11
'e'  => 26
'f'  => 16
'g'  => 7
'h'  => 5
'i'  => 32
'l'  => 16
'm'  => 1
'n'  => 16
'o'  => 7
'p'  => 5
'q'  => 2
'r'  => 22
's'  => 4
't'  => 21
'u'  => 4
'v'  => 3
'x'  => 3
'y'  => 1
'{'  => 3
'}'  => 3