Write a program that reads text from a file, breaks the text into words, counts
ID: 3655077 • Letter: W
Question
Write a program that reads text from a file, breaks the text into words, counts the number of times each word appears in the file, and, for each word, uses a queue of integers, to store the lines number on which each words appears. open input file create concordance line-number := 0 line := readLine while not end of file line-number:= line_number + 1 words := parse(line) for each word in words concordance.add(word, line-number) end for line := readLine end while close file write concordance to a file Order the tree alphabetically. A concordance is a binary search tree. Its node is a stringExplanation / Answer
#include /* counts words in the file */ int main(void) /* C89 ANSI */ { FILE *fp; int r; size_t n; const char *filename = "file"; if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "error: file" " "); return 1; } n = 0; while ((r = fscanf(fp, "%*100s")) != EOF) n++; if (ferror(fp) != 0) { fprintf(stderr, "error: read file" " "); fclose(fp); return 1; } if (n == 1) printf("there is %lu word" " ", n); else printf("there are %lu words" " ", n); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.