/*

 * let_hist.c

 * This program reads an input file and prints out a histogram of all the

 * Latin letters it contains.

 */

 

#include <stdio.h>

 

int main (int argc, char **argv)

{

  FILE   *p_file;              /* The input file. */

  int    c;                    /* The current character read. */

  int    hist['Z' - 'A' + 1];  /* Histogram of all latin letters. */

  int    sum_let;              /* The total number of letters in the file. */

  float  freq;                 /* The frequency of the current letter. */

 

  /* Check the number of arguments to the program: */

  if (argc < 2)

  {

    printf ("Usage: %s <input file>\n", argv[0]);

    return (1);

  }

 

  /* Open the input file, specified by argv[1] */

  p_file = fopen (argv[1], "r");

 

  if (p_file == NULL)

  {

    printf ("Failed to open the input file '%s'.\n", argv[1]);

    return (1);

  }

 

  /* Zero the histogram. */

  for (c = 'A'; c <= 'Z'; c++)

    hist[c - 'A'] = 0;

 

  /* Read the file, character by character. */

  sum_let = 0;

  while ((c = fgetc(p_file)) != EOF)

  {

    /* Change to upper case, if needed. */

    if (c >= 'a' && c <= 'z')

      c += ('A' - 'a');

 

    /* Update the histogram, if we have a letter. */

    if (c >= 'A' && c <= 'Z')

    {

      (hist[c - 'A'])++;

      sum_let++;

    }

  }

 

  /* Close the input file. */

  fclose (p_file);

 

  /* Print the histogram. */

  for (c = 'A'; c <= 'Z'; c++)

  {

    freq = 100 * (float)hist[c - 'A'] / (float)sum_let;

    printf ("%c : %4d    (%5.2f %%)\n", c, hist[c - 'A'], freq);

  }

 

  return (0);

}