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

Please comment code, Thanks! Please use these functions: void my_strcpy(char In[

ID: 3605446 • Letter: P

Question

Please comment code, Thanks!

Please use these functions:

void my_strcpy(char In[], char Out[], int L) {

int i;

for(i = 0; i < L; i++)

Out[i] = In[i];

Out[L] = '';

return;

}

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 versa

Explanation / Answer

Given below is the code with output. I tested it with a sample file I had. If any issues executing the program, post a comment. If it helped, please do rate it . Thank you.


#include <stdio.h>
#include <string.h>
void my_strcpy(char In[], char Out[], int L);
int find(char line[], char search[]); //return index of the first occurence of search string, -1 if not found
int main()
{
char filename[25];
FILE *fp;
char line[200], search[200];
int lineNo = 0;
int count = 0;
  
printf("Enter filename: ");
gets(filename);
  
printf("Enter the search string: ");
scanf("%s", search);
  
fp = fopen(filename, "r");
if(fp == NULL)
{
printf("Couldn not open file %s " ,filename);
return 1;
}
  
while(1)
{
fgets(line, 200, fp);
if(feof(fp))
break;
lineNo++;
  
if(find(line,search) != -1)
{
printf("Found %s on line %d: %s", search, lineNo, line);
count ++;
}
}
  
printf("The string "%s" was found a total of %d times in the files %s ", search, count ,filename);
fclose(fp);
  
}


void my_strcpy(char In[], char Out[], int L) {
int i;
for(i = 0; i < L; i++)
Out[i] = In[i];
Out[L] = '';
return;
}

int find(char line[], char search[])
{
int len1 = strlen(line);
int len2 = strlen(search);
int last = len1 - len2;
int i;
char temp[200];
  
for(i = 0; i <= last; i++)
{
my_strcpy(line+i, temp, len2); //copy len2 chars from line+i into temp
if(strcmp(temp, search) == 0) //if temp and search match, return index
return i;
}
  
return -1;
  
}

Enter filename: getty.txt
Enter the search string: the
Found the on line 1: Fourscore and seven years ago our fathers brought forth on this
Found the on line 2: continent a new nation, conceived in liberty and dedicated to the
Found the on line 4: Now we are engaged in a great civil war, testing whether that nation
Found the on line 8: gave their lives that that nation might live. It is altogether
Found the on line 14: we say here, but it can never forget what they did here. It is for
Found the on line 15: us the living, rather, to be dedicated here to the unfinished work
Found the on line 16: which they who fought here have thus far so nobly advanced. It is
Found the on line 17: rather for us to be here dedicated to the great task remaining
Found the on line 18: before us - that from these honored dead we take increased devotion
Found the on line 19: to that cause for which they gave the last full measure of devotion
Found the on line 20: - that we here highly resolve that these dead shall not have died in
Found the on line 22: freedom - and that government of the people, by the people, for the
Found the on line 23: people, shall not perish from the earth.
The string "the" was found a total of 13 times in the files getty.txt

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