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

NEED HELP WITH THIS. Write a C program named as countD.c that takes input text f

ID: 3822721 • Letter: N

Question

NEED HELP WITH THIS.

Write a C program named as countD.c that takes input text file and a digit as arguments, outputs how many times the digit appears in the file. If the text file does not exist, print out an error message to the terminal. For example, sample outputs could be like below $cat test.txt This is a list of courses. CSC 1010 - COMPUTERS & APPLICATIONS $./countD test. txt 1 '1' appeared 2 times $./countD NotExist. txt 1 Error: file not exist. Put the source code of countD.c in your answer sheet. In your answer sheet, please also attach screenshots of the outputs using the test.txt file in the example. Upload your file countD.c to iCollege.

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[]) {
int k;

FILE *fp = fopen(argv[1], "r");
char c;
int count = 0;
if (fp == NULL)
{
printf("Error: file not exist ");
exit(0);
}
else{
int digitCount = 0, lowerCount = 0, upperCount = 0, specialCount = 0, lineCount=1;
while((c=fgetc(fp))) {
if(c == EOF) {break;}
if(argv[2][0] == c) {
count++;
}
}
printf("'%c' appeared %d times ", argv[2][0], count);
}
fclose(fp);
return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                                

sh-4.2$ ./main text.txt 1                                                                                                                                                                                                                                              

'1' appeared 2 times