Hello,
I'm trying to develop a program that will enable me to count the number of words in a text file. As a plus, I'd like to be able to count how many different words there are too. I have a decent start on the program, but am quite unsure of where to move from here. I know that I need to malloc space for the array, but am not sure how to. Also, I believe that strlen may come into play.
Any help would be greatly appreciated.
Thanks,
James
Code:#include <stdio.h> #include <stdlib.h> #define MAXWORDS 4000 //less than 4000 total words in the //text file char *word[MAXWORDS]; int wordcount[MAXWORDS]; #define MAXWLEN 30 //no words larger than 30 characters char buff[MAXWLEN]; int nwords, totalwords; main() { int i; while(get_word(buff)) { /**** The part where I am stuck on ****/ } for(i = 0; i < nwords; i++) totalwords += wordcount[i]; //if I keep getting //words, the loop will //continue printf("there were %d different words out of %d totalwords\n", nwords, totalwords); } //-----ignore the section below, it defines what a word is to the //-----program #include <ctype.h> /* 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 I have a word */ }



LinkBack URL
About LinkBacks




but he does his job well).