#include <stdio.h>

#define INP_LENGTH 75

FILE  *in_fptr,
      *out_fptr,
      *fopen();
int   count[26];

char  letters[27], str[INP_LENGTH];

main()
{
   int   data_count, k;
   void  count_letters(), print_counts();

   strcpy(letters, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

   if ( (in_fptr = fopen("letters.in", "r")) == NULL )
     {
      printf("*** can't open input file *** \n");
      exit();
     }

   if ( (out_fptr = fopen("letters.out", "w")) == NULL )
     {
      printf("*** can't open output file *** \n");
      exit();
     }

   fscanf(in_fptr, "%d\n", &data_count);

   for ( k = 1;   k <= data_count;   ++k )
     {
      fprintf(out_fptr, "Data set #%d: ", k);
      fgets(&str[0], INP_LENGTH, in_fptr);
      fprintf(out_fptr, str);
      count_letters();
      print_counts();
     }

   if ( fclose(in_fptr) == EOF )
     {
      printf("*** can't close input file *** \n");
      exit();
     }

   if ( fclose(out_fptr) == EOF )
     {
      printf("*** can't close output file *** \n");
      exit();
     }

}/* end main */

/* ************************************************************* */

void count_letters()
{
   int  k, j;

   for ( k = 0;   k <= 25;   ++k )
      count[k] = 0;

   /* go thru each character in "str" */
   for ( j = 0;   j < strlen(str);   ++j )
     {
      if (  ( str[j] != ' ' )  &&  ( str[j] != '\n' )  )
        {/* this character is a letter; find which letter it is */
         /* and increment its count                             */
         for ( k = 0;   letters[k] != str[j];   ++k )
            ;
         ++count[k];
        }
     }

}/* end count_letters */

/* ************************************************************* */

void print_counts()
{
   int  k, j, flag;

   /* go thru each possible count */
   for ( j = 1;   j <= 70;   ++j )
     {
      flag = 1;
      /* go thru each letter and see if it has this count */
      for ( k = 0;   k <= 25;   ++k )
        {
         if ( count[k] == j )
           {/* need to print this letter */
            if ( flag == 1 )
              {/* first letter with this count, need "heading" */
               fprintf(out_fptr, "   Count %2d: ", j);
               flag = 2;
              }
            fprintf(out_fptr, "%c ", letters[k]);
           }
        }/* end for ( k = 0 to 25 ) */

      if ( flag == 2 )
         /* there were letters with this count */
         fprintf(out_fptr, "\n");

     }/* end for ( j = 1 to 70 ) */

   fprintf(out_fptr, "\n");

}/* end print_counts */

/* ************************************************************* */
