I read K & R, currently in chapter 1. After reading the section and trying to solve the problems, I would like to check other solutions on the Internet, just see different methods of solving the same problem.
Exercise 1-14 says that we need to print a histogram of the frequencies of different characters at the input. This solution I found takes into account only the letters of the alphabet:
#include <stdio.h>
#define MAX 122
#define MIN 97
#define DIFF 32
int main(){
int c = EOF;
int i, j;
int array[MAX - MIN];
printf("%d ", MAX - MIN);
for (i = MIN; i <= MAX; i++){
array[i] = 0;
printf("%d ", i);
}
while ((c = getchar()) != EOF){
if (c >= MIN)
++array[c];
else {
++array[c + DIFF];
}
}
for (i = MIN; i <= MAX; i++){
printf("|%c%c|", i - DIFF, i);
for (j = 1; j <= array[i]; j++){
putchar('*');
}
putchar('\n');
}
return 0;
}
, , array[]
. 25 (MAX - MIN).
0 24. 97
122
. , ?
for (i = 0, i < MAX - MIN; i++)
,
array[97] ... array[122]
EDIT:
printf("%d ", MAX - MIN);
printf("%d ", i);
, , 97
.