Advertisement
Guest User

hittogwam

a guest
Dec 18th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. /************************************************************************************************************************************************************
  2.  
  3.     Write a program to print a histogram of the frequencies of different characters of input.
  4.  
  5.     NOTE : we only consider alphanumeric characters here      
  6.  
  7. ************************************************************************************************************************************************************/
  8.  
  9. #include<stdio.h>
  10.  
  11. #include<ctype.h>
  12.  
  13. int main (int agrc, char * argv[]) {
  14.  
  15.     int CHARACTERS [37];
  16.  
  17.     int i,c,k,a,j;
  18.  
  19.     for (i = 0; i <= 36; i++)
  20.  
  21.         CHARACTERS[i] = 0;
  22.  
  23.     while ((c = getchar()) != EOF) {
  24.  
  25.         if (isdigit(c)) {
  26.  
  27.             k = c -22;
  28.  
  29.             CHARACTERS[k]++;
  30.  
  31.                     }
  32.  
  33.         else if (isalpha(c)) {
  34.  
  35.             a = tolower(c);        
  36.        
  37.             k = a - 'a';
  38.  
  39.             CHARACTERS[k]++;
  40.  
  41.                      }
  42.  
  43.                        }
  44.  
  45.         printf("\nFrequency of occurence\n\n");
  46.  
  47.             for (i = 60; i >= 1; i--) {
  48.  
  49.                             printf("%d\t|    ",i);
  50.  
  51.                     printf(" ");
  52.  
  53.                     for (j = 0; j <= 36; j++) {
  54.  
  55.                         printf("  ");
  56.  
  57.                         if (CHARACTERS[j] >= i) {
  58.  
  59.                             printf("*");
  60.  
  61.                                      }
  62.  
  63.                         else {
  64.  
  65.                             printf(" ");
  66.  
  67.                               }
  68.                                        }
  69.  
  70.                    
  71.  
  72.                    
  73.                     printf("\n");
  74.  
  75.                             }
  76.  
  77.             printf("\t");
  78.  
  79.                             for (i = 0; i < 120; ++i) {
  80.  
  81.  
  82.                             printf("-");
  83.  
  84.                                           }
  85.  
  86.            
  87.             printf("\n\t\ta  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z  0  1  2  3  4  5  6  7  8  9\n\n");
  88.  
  89.             printf("\tCHARACTERS\n");
  90.                          
  91.  
  92.  
  93.  
  94.                    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement