Please Comment code, THANKS! C code: Problem 1 (35 points): grep We wish to crea
ID: 3605226 • Letter: P
Question
Please Comment code, THANKS!
C code:
Problem 1 (35 points): grep We wish to create a utility that will search a text file for a specified string. Unix has a line utility that does this called grep, which is a funny acronym that stands for globally searchoa regular expression and print. Write a C program that will search a text file for a specified string. Your program should a) Prompt the user for the name of the file to be searched. b) Prompt the user for the string to be found in the file. c) Print the name of the file searched. d) If the string is found then print the entire line of text where the string was found and the line number. e) Print the number of times the string was found Assumptions for this assignment Assume that all strings have a maximum length of 200 characters .Assume that each line of the file being searched is no more than 200 characters long. . Assume case sensitivity For example, if the file being searched contains OK and the string to find is ok thena match is not found; i.e. you do not need to worry about changing uppercase characters to lowercase or vice versaExplanation / Answer
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char filename[]="name.txt",line[200],search_string[]="12";
fp=fopen(filename,"r");
if(!fp)
{
perror("could not find the file
");
exit(0);
}
while ( fgets ( line, 200, fp ) != NULL ) /* read a line */
{
if(strstr(line,search_string))
fputs ( line, stdout ); /* write the line */
}
fclose ( fp );
return 0;
}