wordocc.c (1088B)
1 /* Exercise 8.34 (b) */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #define SIZE 50 8 #define BUF 255 9 #define MAXWORD 30 10 11 int main(void) 12 { 13 char string[SIZE][BUF] = { { 0 } }, *tokp; 14 int count[MAXWORD] = { 0 }; 15 int i = 0, highest = 0; 16 17 /* start to take the strings */ 18 printf("str (%d to end): ", EOF); 19 gets(string[i]); 20 21 /* continue to take the strings */ 22 while( atoi(string[i++]) != EOF ) { 23 printf("str (%d to end): ", EOF); 24 gets(string[i]); 25 } 26 string[i - 1][0] = '\0'; /* clear the EOF char */ 27 28 /* count the lenght for each word */ 29 for(i = 0; i < SIZE; i++) { 30 31 /* tokenizing.. */ 32 tokp = strtok(string[i], " "); 33 while( tokp != NULL ) { 34 ++count[(int)strlen(tokp)]; 35 36 /* this instruction is not optimized */ 37 if((int)strlen(tokp) > highest) 38 highest = (int)strlen(tokp); 39 40 tokp = strtok(NULL, " "); 41 } 42 } 43 44 /* print the results */ 45 printf("Word size\tOccurrences\n"); 46 for(i = 1; i <= highest; i++) { 47 printf("%9d\t\t%3d\n", i, count[i]); 48 } 49 50 return 0; 51 } /* E0F main */ 52