Question
file.c code : #include #include #include #include char alphabets[]= {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
file.c code :
#include
#include
#include
#include
char alphabets[]= {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int countString(const char *fileName, char *str);
int countEmptyLines(const char* fileName);
int main(int argc, char *argv[]){
if (argc != 3){
printf("Incorrect Parameters. ");
exit(0);
}
int word_count = countString(argv[1], argv[2]);
int empty_line = countEmptyLines(argv[0]);
printf("The string \"%s\" occurs %d times in File \"%s\". ", argv[2], word_count, argv[1]);
printf("File \"%s\" has %d empty lines. ", argv[1], empty_line);
return 0;
}
int countString(const char* fileName, char *str){
FILE *f1;
int word_count = 0;
f1 = fopen(fileName, "r");
if (f1 == NULL){
perror("Cannot open files .....");
exit(EXIT_FAILURE);
}
// Insert your code here!
fclose(f1);
return word_count;
}
int countEmptyLines(const char *fileName)
{
int emptyLine = 0;
FILE *fp = fopen("poem.txt", "r");
if (fp == NULL) {
printf("Error: Could not open specified file! ");
return -1;
}
// Insert your code here!
return emptyLine;
}
poem.txt file:
Do not go gentle into that good night, Old age should burn and rave at close of day; Rage, rage against the dying of the light.
Though wise men at their end know dark is right, Because their words had forked no lightning they Do not go gentle into that good night.
Good men, the last wave by, crying how bright Their frail deeds might have danced in a green bay, Rage, rage against the dying of the light.
Wild men who caught and sang the sun in flight, And learn, too late, they grieved it on its way, Do not go gentle into that good night.
Grave men, near death, who see with blinding sight Blind eyes could blaze like meteors and be gay, Rage, rage against the dying of the light.
And you, my father, there on the sad height, Curse, bless, me now with your fierce tears, I pray. Do not go gentle into that good night. Rage, rage against the dying of the light.
You need to write a program that will read a file name and a string from the command line. Next, the program will find the number of occurrences of the string in that file and count the number of empty lines too. Please find attached the C code "file.c". You need to implement the following two functions: int countString(const char* fileName, char *str); I counts the number of occurrences of "str" in file "fileName int countEmptyLines(const char* fileName); counts the number of empty lines in file "filename" 8 points pointsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started