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

Text is : Four score and seven years ago our fathers brought forth on this conti

ID: 3605862 • Letter: T

Question

Text is:

Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in liberty, and dedicated to
the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that
nation, or any nation so conceived and so dedicated, can long
endure. We are met on a great battlefield of that war. We have
come to dedicate a portion of that field, as a final resting
place for those who here gave their lives that that nation might
live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate, we can not
consecrate, we can not hallow this ground. The brave men, living
and dead, who struggled here, have consecrated it, far above our
poor power to add or detract. The world will little note, nor long
remember what we say here, but it can never forget what they did
here. It is for us the living, rather, to be dedicated here to the
unfinished work which they who fought here have thus far so nobly
advanced. It is rather for us to be here dedicated to the great
task remaining before us-that from these honored dead we take
increased devotion to that cause for which they gave the last full
measure of devotion-that we here highly resolve that these dead
shall not have died in vain-that this nation, under God, shall have
a new birth of freedom-and that government of the people, by the
people, for the people, shall not perish from the earth.

Problem 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 and print. 1 (35 points): grep 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 sensitivitv. 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.

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 thefilw 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