/* Asn 3 example solution */ /* Your task for next week is to build a "ragged" array of all of the unique words from Alice in Wonderland and keep track of the count of number of times each of those words appears in the story. Your result should agree with this ... $ gcc asn03.c $ a.out < alice30.txt there were 2949 unique words out of 29113 totalwords */ #include #include #define MAXWORDS 20000 char *word[MAXWORDS]; int wordcount[MAXWORDS]; #define MAXWLEN 30 char buff[MAXWLEN]; int nwords, totalwords; main() { int i; while(get_word(buff)) { for(i = 0; i < nwords; i++) if(strcmp(word[i], buff) == 0) break; if(i == nwords) word[nwords++] = (char *)strdup(buff); wordcount[i]++; } for(i = 0; i < nwords; i++) totalwords += wordcount[i]; printf("there were %d unique words out of %d totalwords\n", nwords, totalwords); } #include /* Leave this routine EXACTLY as it stands */ int get_word(char *s) { /* s is where to make the string */ int c; do { /* skip non alpha chars */ c = getchar(); if(c == EOF) return(0); /* end of file marker means no word */ } while(!isalpha(c) && !isdigit(c)); do { /* string is consecutive alpha num */ if(isupper(c)) c = tolower(c); *s++ = c; c = getchar(); } while(isalpha(c) || isdigit(c)); *s = 0; /* null terminate the string */ return(1); /* indicate that we got a word */ }