Hey
I have a piece of code that counts the number of words input by the user using strtok. However, I am struggling with converting this program to do the same thing but instead of the user inputting the text, it will read from a text file, counting the number of words in the text file.
Ne tips would be greatly appreciated.
Code:#include <stdio.h> #include <string.h> #define MAX_LINE 80 int main(void) { char line[MAX_LINE]; char delim[] = " ,.!;:\n"; /* sets up word separators*/ char *words, *ptr; int count = 0; printf("Enter line of text \n"); fgets(line,MAX_LINE,stdin); ptr= line; while ((words = strtok(ptr, delim)) !=NULL) {printf("\n%s\n",words); count++; ptr = NULL; } printf("there were %d words in the string \n",count); return 0; }



LinkBack URL
About LinkBacks


