Subject: CHAR.C --- A Piece of Utility

From: Rupert Zhu <[email-masked]>
Date: Thu, 7 Feb 91 21:56:26 EST

I used the following program to produce statistics of bytes information
in any file, text or binary.  It is helpful in analyzing file contents.

-- Rupert

--------------------------- CUT HERE ------------------------------
/*  char.c  */
/* Show the statistics on all ASCII characters 0--255   */
/* Author: Rupert Zhu    December/1990        */

#include  <stdio.h>
#include  <ctype.h>
#include  <math.h>
/* #define   ceil(X)    ( floor(X + 0.5) )    */

long   buffer[260];    /* buffer[i]== number of occurence of code i */
long   counter;       /* Count total number of characters  */
long   acount, bcount;  /* Count ordinary ASCII and extended ASCII */

void   printout(void);

main()
{
	int    c, i, j, count;

	for (i=0; i<=259; i++)
		buffer[i] = 0;

	counter = 0;            /*  the EOF character not counted  */
	while ( (c=getchar()) != EOF) {
			buffer[c]++;
			counter ++;
		}
/*	buffer[c]++;     */
	acount = 0;
	bcount = 0;
	for (i=0; i <= 127; i++)
		acount = acount + buffer[i];
	for (i=128; i <= 255; i++)
		bcount = bcount + buffer[i];
	printf("\nTotal Number of      All       Characters:  %ld", counter);
	printf("\nTotal Number of Ordinary ASCII Characters:  %ld", acount);
	printf("\nTotal Number of Extended ASCII Characters:  %ld\n", bcount);
	printout();
}

void    printout(void)
{
	int    c, i, j, count, depth;
	long   ctab[2][260];

	printf("\n\n\n");
	for (i=0; i<=259; i++) {
		ctab[0][i] = 0;
		ctab[1][i] = 0;
		}
	j = 0;
	for (i=0; i<=259; i++)
		if (buffer[i] >= 1) {
			ctab[0][j] = i;
			ctab[1][j] = buffer[i];
			j++;
			}
	count = j;
	depth = ceil(count/5.0);
	printf("\t   Frequency of Occurence of Each ASCII Character\n\n");
	printf("Code Occurence   ");
	printf("Code Occurence   ");
	printf("Code Occurence   ");
	printf("Code Occurence   ");
	printf("Code Occuren\n");
	for (i=0; i<= depth-1; i++) {
       	    printf("%3ld%7ld       ", ctab[0][i], ctab[1][i]);
	    printf("%3ld%7ld       ", ctab[0][i+depth], ctab[1][i+depth]);
	    printf("%3ld%7ld       ", ctab[0][i+2*depth], ctab[1][i+2*depth]);
	    printf("%3ld%7ld       ", ctab[0][i+3*depth], ctab[1][i+3*depth]);
	    printf("%3ld%7ld\n",      ctab[0][i+4*depth], ctab[1][i+4*depth]);
	    }
}
/* --------------------------- THE END ----------------------------- */