Question: In this lab you will write a C program to create a simplified version of the grep command. Grep was written by Ken Thompson, one
In this lab you will write a C program to create a simplified version of the grep command.
Grep was written by Ken Thompson, one of the creators of Unix at Bell Labs. Grep searches one or more files for a regular expression, and outputs all lines that contain the expression. A regular expression is a pattern which represents one or more strings. Thus grep is a utility that prints all lines of a file that contain a string that matches the given pattern.
Our simplified version of grep will not use a regular expression. Instead we will have a string and a file, and we will print all lines of the file that contain the string. The only extension will be that our match will be case insensitive.
For example, given the file hobbit.dat:
Bilbo Baggins left his place and went and stood on a chair under the illuminated
tree. The light of the lanterns fell on his beaming face; the golden buttons
shone on his embroidered silk waistcoat. "My dear Bagginses and Boffins", he
began, "and my dear Tooks and Brandybucks, and Grubbs, and Chubbs, and
Burrowses, and Hornblowers, and Bolgers, Bracegirdles, Goodbodies, Brockhouses,
and Proudfoots. Also my good Sackville-Bagginses that I welcome back at last to
Bag End."
Given the pattern baggins and the file hobbit.dat, our grep command will print:
Bilbo Baggins left his place and went and stood on a chair under the illuminated
shone on his embroidered silk waistcoat. "My dear Bagginses and Boffins", he
and Proudfoots. Also my good Sackville-Bagginses that I welcome back at last to
Requirements
Write a function to convert a string to lower case. Use the ASCII codes to check whether a character is upper case, and if so, convert it to lower case. Use pointers in this function for your string; do not use array brackets.
Write a function to compute and return the length of a string. Use pointers in this function for your string; do not use array brackets.
Write a function to search for a newline (' ') in a string and replace it with a null byte ('\0'). Use pointers in this function for your string; do not use array brackets.
Write a function with two parms: the word and a line from the file. This function should return true if the word is found anywhere within the line. You might want to have it call another function to help; that's what I did. It can help you figure out the logic if you have each function do part of the task. Use pointers in this function for your string; do not use array brackets.
Write a main function to do the following:
Create an array for the string (size 40) and a line of the file (size 300).
Prompt the user to enter the string and the filename. Call fopen to open the file.
Create a loop to check each line of the file; if the line contains the string, print the line.
Do not use any of the C string library functions in your program.
Hints
Look up fopen. You will use "r" for the mode parameter; this opens the file for reading. The FILE pointer returned by fopen is used as a parm for fgets so that it reads from your file.
Look up fgets. fgets reads one line from the file, like nextLine() in Java.
fgets returns NULL when it reaches end of file.
If the length of the line is less than the size parameter, fgets includes the newline in the string it returns. Replace the newline with a null byte before checking the line.
A character variable contains the ASCII code for the character, which is a number. In C you can compare a char variable to a number; it uses the ASCII code in the comparison. You can also add or subtract from a character; this changes the ASCII code. Use this idea in your function to convert to lower case.
Put your data file in the same directory as your code.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
