3 * gcc charfreq.c -o charfreq -Wall -W -Wextra -ansi -pedantic
10 int main(int argc
, char *argv
[])
13 * When we partially initialize an array,
14 * C automatically initializes the rest of it
15 * to 0, NULL, etc, depending on the element type
17 * That said, the following is adequate:
19 unsigned int freq
[26] = { 0 }; /* Frequencies for 'A' to 'Z' */
20 unsigned int i
, j
, len
, maxf
;
24 /* Check argument count */
26 fprintf(stderr
, "Usage: %s path\n", argv
[0]);
30 /* Open file for reading */
31 if ((fp
= fopen(argv
[1], "r")) == NULL
) {
36 /* Count frequencies */
37 while ((c
= fgetc(fp
)) != EOF
) {
39 if (c
>= 'A' && c
<= 'Z')
43 /* Calculate size of array */
44 len
= sizeof freq
/ sizeof *freq
;
46 /* Get max frequency */
48 for (i
= 1; i
< len
; i
++)
52 /* Print frequencies */
54 for (i
= freq
[maxf
]; i
> 0; i
--) {
56 for (j
= 0; j
< len
; j
++)
66 for (i
= 0; i
< len
; i
++)
67 printf("%c", (char)('A' + i
));
73 * (since we opened the file only for read,
74 * we assume that it is safe to not check against
75 * the return value of fclose())