/* freq.c */

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

int main(int argc, char **argv) 
{
	int char_set[128];
	FILE *input = 0;
	int temp, i, count = 0;

	if (argc != 2) 
	{
		printf("usage: %s input_file\n", argv[0]);
		exit(-1);
	}

	input=fopen(argv[1], "r");
	if (input == NULL) 
	{
		printf("Error opening %s\n", argv[2]);
		exit(1);
	}

	memset(char_set, 0, 128 * sizeof(int));

	while (!feof(input)) 
	{
		count ++;
		temp = fgetc(input);		
		if ( (isascii(temp)) && (!isspace(temp)) ) 
		{
			char_set[temp] ++;
		}
	}

	printf("CHAR	COUNT\n");
	while (count > 0)
	{
		for (temp  = 0; temp < 128; temp ++) 
		{
			//if (char_set[temp] != 0)
			if (char_set[temp] == count)
			{
				printf("%4c %7d ", temp, char_set[temp]);
				for (i = 0; i < char_set[temp]; i ++) 
				{
					printf("*");
				}
				printf("\n");
			}
		}
		count --;
	}
}

