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

This program must be coded using C language and compile with basic gcc compile c

ID: 3634332 • Letter: T

Question

This program must be coded using C language and compile with basic gcc compile command within linux.

 

Develop a C program called charcount.c that reads a filename
from a user and determines the number of characters in the file.
In the program you should use standard C library function(s).
[Hint: http://www.cplusplus.com/reference/clibrary/cctype/]

The following shows a sample run of your program:
$ cat file.txt
Hi there
Welcome
$ gcc -o charcount charcount.c
$ ./charcount
Enter a filename: file.c
The file doesn't exist.
$ ./charcount
Enter a filename: file.txt
The file has 14 characters.

Explanation / Answer

Dear, /*This program counts the number of characters (alphabets) in a file*/ /*Header files*/ #include /*header file for I/O operations*/ #include /*headet file for isalpha function*/ /*main function*/ int main() { /*filepointer type variable to hold file data*/ FILE *fp; /*variables required for processing*/ int n=0; char ch, filename[80]; printf("Enter a filename: "); /*read a file name*/ scanf("%s", filename); /*to open file*/ fp = fopen(filename, "r") ; /*check file exist or not*/ if(fp == NULL) { printf("The file doesn't exist."); exit(0); } /*loop to count the characters in a file*/ do { ch = fgetc(fp) ; if(isalpha(ch)) n++; }while(ch != EOF); /*display out put*/ printf("The file has %d characters.",n); } Output: $ cat testfile.txt Hi there Welcome to Cramster $ gcc -o charcount charcount.c $ ./charcount Enter a filename: testfile The file doesn't exist. $ ./charcount Enter a filename: testfile.txt The file has 24 characters. Hope this would help you.
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