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;

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" ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.