countwords.c (794B)
1 /* Exercise 8.20 */ 2 3 #include <stdio.h> 4 #include <string.h> 5 #include <stdlib.h> 6 7 #define STRINGS 10 8 #define BUF 254 9 10 int main(void) 11 { 12 char string[STRINGS][BUF] = { { 0 } }, *tokp; 13 int i = 0, counter = 0; 14 15 /* Take the strings */ 16 printf("Give me a string (%d to end): ", EOF); 17 gets(string[i]); 18 19 while( atoi(string[i++]) != -1 ) { 20 printf("Give me a string (%d to end): ", EOF); 21 gets(string[i]); 22 } 23 24 string[i - 1][0] = '\0'; /* Remove the '-1' */ 25 26 for(i = 0; i < STRINGS; i++) { 27 tokp = strtok(string[i], " "); 28 29 while( tokp != NULL ) { 30 ++counter; 31 tokp = strtok(NULL, " "); 32 } 33 34 } 35 36 if(!counter) 37 printf("There are no words!\n"); 38 else 39 printf("There are %d words!\n", counter); 40 41 return 0; 42 } /* E0F main */ 43