0

WHERE DO I START!?

Write a mygrep program that accepts arguments:
mygrep [-i] [-n] match file1 [file2] [file3] ... [filen]

A grep program will look for occurences of the "match" string in 
the files file1, file2, ... filen.  Whenever the "match" string is 
found, the line where it is found is printed out.  The option -i 
tells the grep program to make its string comparisons in a 
case insensitive manner.  The option -n tells the grep program to 
print out the line number in the file where the match string is found
along with the file's line.  For example, suppose that a file1.txt 
contains:

one time around the
block is enough 
for me. See me in the IS department. 

Assume the name of your program is mygrep, then executing the command:
mygrep is file1.text 

will generate:

file1.txt: block is enough

mygrep -i -n is file1.text 
will generate:

file1.txt:2: block is enough
file1.txt:3: for me.  See me in the IS department.


mygrep can also process multiple files in the same command.  
If any of this is still confusing, please ask. 




HINTS: strstr, tolower  . . . Consider the following tolower example:

   char * x="ABCDEF";
   char buff[100];
   int len = strlen(x);

   // Note the following loop copies the null 
   // terminator
   for (int i=0; i <= len; i++)
       buff[i] = tolower(x[i]);
   cout << buff<<endl;
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by vijayan121
0

to get the command line elements, use the args passed to main:

int main( int argc, char** argv )
{
  copy( argv+1, argv+argc, 
        ostream_iterator<const char*>(cout,"\n") ) ;
}

to read a file line by line, use ::getline

int main( int argc, char** argv )
{
  ifstream file(__FILE__) ;
  string line ;
  int line_number = 0 ;
  while( getline(file,line) )
    cout << ++line_number << ": " << line << '\n' ;
}

to see if a string contains another string, use string::find

int main( int argc, char** argv )
{
  string line = "#include <algorithm>" ;
  string looking_for = "include" ;
  if( line.find(looking_for) != string::npos )
    cout << "found it\n" ;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.