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;

Dani AI

Generated

The simplest, robust plan is: parse the optional flags (-i and -n), take the next argument as the literal match string, then treat remaining arguments as filenames. For every file open it, read lines, test each line for the match (case-sensitively or not depending on -i), and print matching lines in the format shown in the original post (filename:line or filename:linenumber: line). This follows the examples given by .

Option parsing should stop at the first non-option argument (or when -- is seen) so a match that begins with - is handled correctly. Support bundled flags (e.g. -in or -ni) by scanning characters in any - argument. If no files are supplied, print a usage message and exit. When opening files, check for errors, write a short diagnostic to cerr, and continue to the next file.

A safe, portable way to do case-insensitive substring search without copying whole lines is to use std::search with a character-comparison predicate that lowercases on-the-fly (cast to unsigned char before calling std::tolower to avoid UB):

#include <algorithm>
#include <cctype>
#include <string>

bool ci_contains(const std::string& hay, const std::string& needle) {
    if (needle.empty()) return true;
    auto it = std::search(
        hay.begin(), hay.end(),
        needle.begin(), needle.end(),
        [](char a, char b){
            return std::tolower(static_cast<unsigned char>(a)) ==
                   std::tolower(static_cast<unsigned char>(b));
        });
    return it != hay.end();
}

Practical notes: use std::getline for line reads (as recommended) and keep a 1-based line counter. Trim a trailing '\r' from lines if input may be CRLF. For performance on very large files, pre-lowercase the pattern once and lowercase each line with std::transform instead of per-character comparisons. Handle empty pattern, unreadable files, and binary input gracefully so the tool behaves predictably.

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.