training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

textanalysis.c (1109B)


      1 /* Exercise 8.34 (a) */
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <stdlib.h>
      6 #include <ctype.h>
      7 
      8 #define STRINGS 10
      9 #define BUF 254
     10 #define ASCII 127
     11 
     12 int main(void)
     13 {
     14    char string[STRINGS][BUF] = { { 0 } };
     15    int i = 0, j, counter[ASCII] = { 0 };
     16 
     17    /* Take the strings */
     18    printf("Give me a string (%d to end): ", EOF);
     19    gets(string[i]);
     20 
     21    while( atoi(string[i++]) != EOF ) {
     22       printf("Give me a string (%d to end): ", EOF);
     23       gets(string[i]);
     24    }
     25 
     26    string[i - 1][0] = '\0'; /* Remove the '-1' */
     27 
     28    for(i = 0; i < STRINGS; i++) {
     29       for(j = 0; (unsigned)j < strlen(string[i]); j++) {
     30 	 ++counter[tolower((int)string[i][j])];
     31       }
     32    }
     33 
     34    printf("\nOccurrences table\n"
     35 	  "* (NP = not printable)\n"
     36 	  "* (SP = spaces)\n\n");
     37  
     38    for(i = j = 1; i < ASCII; i++) {
     39       if(counter[i]) {
     40 
     41 	 if(i < 32) printf("NP: ");
     42 	 else if(i == 32) printf("SP: ");
     43 	 else printf("%2c: ", i);
     44          printf("%2d | ", counter[i]);
     45 
     46          if( !(j % 4) )
     47 	    printf("\n-----------------------------------\n");
     48 	 ++j;
     49       }
     50    }
     51    printf("\n\n");
     52 
     53    return 0;
     54 } /* E0F main */
     55