Ok, so the idea of this program is that I have a file. For example, one that has a song, row row row your boat....etc. And I need to count the number of words in that file. If the word repeats, then I count it the number of times it repeats. And then write the output to a file that would go something like:Code:#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define FLUSH while (getchar() != '\n') #define MAXFILESIZE 100 char getData(FILE* fp); int main(void) { FILE* fp; char filename[80]; char terminate[5] = "QUIT"; int quit = 1; printf("\t\t Homework 4 - Pointers and Strings:\n"); do { printf("Enter a filename (Enter'QUIT' to quit):\n"); scanf("%s", filename); FLUSH; quit = strcmp(filename, terminate) == 0; if(!quit) { fp = fopen(filename, "r"); if(fp) { printf("Open success!\n"); getData(fp); fclose(fp); } else { printf("Failed...\n"); } } } while(!quit); } char getData(FILE* fp) { int i; char *ptemp; char **plist; char *list[MAXFILESIZE]; char temp[100]; char wordCounter; *list = (char *)malloc(MAXFILESIZE * sizeof(char)); if(*list == NULL) { printf("NOT ENOUGH MEMORY!\n"); exit(100); } while(fscanf(fp, "%[^ ' ' ^ \n]", temp)); if(ispunct(temp[strlen(temp) - 1 ])) { (temp[strlen(temp) - 1]) = '\0'; } { for(i = 0; i <strlen(temp); i++) { temp[i] = tolower(temp[i]); } printf("%s\n", temp); } *list = temp; printf("%s\n", *list); }
row 3
your 1
boat 1
Also, the case should not affect it(I already took care of this) and the punctuation of the end( like the ',') should be removed(again, I took care of this).
For starters, I'm having trouble reading in more than just the first word... Not sure how to make a loop that would work. Lastly, I have NO idea how to get the counter to work... Especially for repeating words(how to check it)...
Anyway, thank you for reading this and for your time.