Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, if you can do this in C, that would help me out, thanks. Write a program tha

ID: 3634342 • Letter: H

Question

Hi, if you can do this in C, that would help me out, thanks.

Write a program that prints the number of words found in a text file. Words are defined as sequences of characters separated by white space. (Hint: white space is defined as in the fscanf function when reading a string). The program should prompt the user for the name of the file to be analyzed. It is assumed that the words in the text file are all shorter than 256 characters, and that the name of the text file is shorter than 64 characters.

The program should reproduce the following format:

Enter the file name: myfile.txt
myfile.txt contains 167 words

Explanation / 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; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote