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

Problem 1 (35 points): grep We wish to create a utility that will search a text

ID: 3605847 • Letter: P

Question

Problem 1 (35 points): grep We wish to create a utility that will search a text file for a specified string. Unix has a command line utility that does this called grep, which is a funny acronym that stands for globally search a regular expression andprint 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 ssumptions for this assignme 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 then a match is not found; i.e. you do not need to worry about changing uppercase characters to lowercase or vice versa. . Here is some help to get you started reminder that fopen can take a variable argument: printf(" Enter a file name: In"); gets(fname); fp fopen(fname, "r"); A function that you might find useful is: void my_strcpy(char In], char Out, int L) nt 1, for(i = 0; 1

Explanation / Answer

Here is code written in C documented with comments. Here file is taken as Poem,txt in sample run.

hello hhhl
ljlj kjlk
kjl;k ljkl;
this is word counting in the
filw thanks for your supoiert
helo
yeah
hello this is sample text for you
this line also have hello
so what are you

noe the wait is over hello dear
aa

// start of code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main ()
{
   char filename[30], buffer[255], search[25];
   int line=0; // for counting the lines in file
   FILE* fp; // for opening file

   printf("Enter File name: ");
   scanf("%s",filename);// inputing file

   if((fp = fopen(filename, "r")) == NULL) { // in case file was not able to open
       printf("Error opening ");
       return(-1);
   }

   printf("Enter the searching string: ");
   scanf("%s",search);
   printf("Word enetered for search is: %s ", search );      


   while(fgets(buffer, 255, (FILE*) fp)) // opening file till end
   {
       line++; // counting lines
            char *pch = strstr(buffer, search); // checking substring using function
            if (pch) // if search sucessfull
               printf("Found %s on line %d : %s ",search,line,buffer ); // to print the seached word

    }

    return 0;

}
//end of code

Output of the code

user@pc:~$ gcc search_word.c -o search_word
user@pc:~$ ./search_word
Enter File name: Poem.txt
Enter the searching string: hello
Word enetered for search is: hello


Found hello on line 1 : hello hhhl

Found hello on line 7 : hello this is sample text for you

Found hello on line 8 : this line also have hello

Found hello on line 10 : noe the wait is over hello dear

user@pc:~$

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