#include <stdio.h>
#include <ctype.h>

/* compile and run with
   $ gcc -Wall char-hist.c -o char-hist
   $ ./char-hist | grep FOR_PLOT > char-hist.dat
   $ gnuplot
   gnuplot> plot [33:128] 'char-hist.dat' using 3 with histograms
 */

int main()
{
  int c, i, j;
  long int counts[255];

  for (c = 0; c < 255; ++c) {
    counts[c] = 0;
  }

  while ((c = getchar()) != EOF) {
    /* for each character we read we add a count to the histogram */
    ++counts[c];
  }

  /* output a table */
  for (c = 0; c < 255; ++c) {
    printf("%3d   %c   %ld\n", c, isprint(c) ? c : ' ', counts[c]);
  }

  /* now output the counts in a different way */
  for (i = 0; i < 16; ++i) {
    for (j = 0; j < 16; ++j) {
      printf("%4ld ", counts[i*8 + j]);
    }
    printf("\n");
  }

  /* output a table for plotting */
  for (c = '0'; c < 255; ++c) {
    printf("FOR_PLOT: %3d    %ld\n", c, counts[c]);
  }

  return 0;
}

