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

Write a C program that will process one or more files and print out various stat

ID: 3707316 • Letter: W

Question

Write a C program that will process one or more files and print out various statistics for these files. The file names should be provided via command-line arguments or the program will simply use stdio streamto get input (if there are no command-line arguments).If the user does not provide any command line arguments, the input should be read from the stdio stream instead of a standard file. If the user provides one or more command-line arguments, the pro-gram should process files with the names provided by command-line arguments one by one, and printout statistics for each file separately. If more files are processed, summary statistics should also beprinted out.The program should report the following statistics about each file (also summary statistics if multiplefiles are processed):

i) Number of characters in the file.

ii) Number of lines in the file (i.e. number of newline characters).

iii) Number of words in the file.  

A word is a sequence of any characters except for the space,new line or tabulator characters not separated by these characters. Therefore, in a string "This is the first line This is the 2nd" we would have 9 words ( and are the special characters).The program will be similar to the wc command provided in Linux(test it to get an idea). I’d suggest touse the functions defined in ctype.h. For example, isspaceis a good function to identify white spaceand distinguish it from the rest (other characters form words)

Explanation / Answer

The C program is given below. Just copy paste on your favorite IDE, compile and run. This program will open and read a file, count the characters, words and lines. When you run the code, it will prompt to give the name of the file (make a file before that and enter for example file.txt). It will count the number what you need. In the line 30 where it counts the words, you can change the definition of a word according to your need. Try and let me know if you need anything more in the comments. All the best.

#include <stdio.h>

int main()

{

FILE *fp;

char filename[100];

char ch;

int lines, words, chars;

lines = 0; // The counter variable for line is initialized

words = 0; // The counter variable for words is initialized

chars = 0; // The counter variable for characters is initialized

// For prompting to enter filename (for example file.txt)

printf("Enter a filename :");

gets(filename);

// Open the file in read-only mode

fp = fopen(filename,"r");

// If file opened successfully, then write the string to file

if ( fp )

{

//TO repeat until End Of File (EOF) character is reached.

while ((ch=getc(fp)) != EOF) {

// Character count is increased if there is no new line or space

if (ch != ' ' && ch != ' ') { ++chars; }

// word count is incremented if there is a space or a line or a full stop of a comma , you can add more

if (ch == ' ' || ch == ' ' || ch == '.' || ch == ',') { ++words; }

// Line count is increased for every new line

if (ch == ' ') { ++lines; }

}

if (chars > 0) {

++lines;

++words;

}

}

else

{

printf("Failed to open your file, try again!!! ");

}

printf("Lines : %d ", lines);

printf("Words : %d ", words);

printf("Characters : %d ", chars);

getchar();

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