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

/* program to count letter frequencies,
   and print a small histogram.
   burt rosenberg, 27 aug 93
   revised 29 aug 04
*/

#define NUM_LET 26

int cmpfc( const void * i, const void * j ) {
  int ii = * (int*) i ;
  int ij = * (int*) j ;
  return ij-ii ;
}

struct ae { int count; char let ; } ;

main( int argc, char * argv[] ) 
{
  struct ae a[NUM_LET] ;
  int i, j, c ;
  int total, most ;
  int sortedView = 0 ;
  int silent = 0 ;

  if ( argc>1 && *argv[1]=='-' ) switch ( *(argv[1]+1) ) {
    case 's':
       sortedView = 1 ;
       silent = 0 ;
       break ;
    case 'S':
       sortedView = 1 ;
       silent = 1 ;
       break ;
    default :
       printf("usage: %s [-s|-S]\n", argv[0] ) ;
       exit(0) ;
  }

  for (i=0;i<NUM_LET;i++) {
    a[i].count = 0 ;
    a[i].let = i+'a' ;
  }

  c = getchar() ;
  while ( c!=EOF ) {
    if (isalpha(c))
       a[tolower(c)-'a'].count++ ;
    c = getchar() ;
  }

  total = most = 0 ;
  for (i=0;i<NUM_LET;i++) {
    if (most<a[i].count) most = a[i].count ;
    total += a[i].count ;
  } 

  if ( sortedView==1 ) 
    qsort( a, sizeof(a)/sizeof(a[0]), sizeof(a[0]), cmpfc ) ;

  for ( i=0; i<NUM_LET; i++ ) {
    if ( silent ) {
      printf("%c", a[i].let) ;
    }
    else {
      printf("%3d", (100 * a[i].count)/ total ) ;
      printf(" %c ", a[i].let) ;
      for ( j=0; j<(100*a[i].count/total); j++ ) 
       printf("*") ;
      printf("\n") ;
    }
  }
  if ( silent ) {
      printf("\n") ;
  }
  if ( sortedView==0 ) 
    printf("(use -s option for a sorted view)\n") ;
 
}
