Greetings,

I need to develop an application which allows tree main features: to search a text file line by line and if it finds the text then it prints the whole line, so for example lets say the text file has:

abcd
efgh

and i would run the applications with ./igrep efg then it would print efgh.

Now that first part is quite simples and i've managed to get a working version using strstr but the application must also support "gaps", so lets say for example we had the following text file:

bane
bone
bath

And when you run the application with ./igrep b.ne i would print both bane and bone. And also with the following example:

google.com
googlebcom

If i call with ./igrep google.com then it would only print google.com.

As i said before the first part is simple, i have it working but i just don't have a clue how to go around to doing the second and trird part so any help would be most appreciated.

For the second part you could use a regexp (regular expression) library so that you would search for either bd or b?d, which * means any number of characters, and ? means only one character. So b?ne would find both bone and bane, whiew be whould find all the strings that start with the letter b and end with the letter e.

You could write your own simple reg expression function or use one of the existing libraries, such as this one:
http://www.gnu.org/software/libc/manual/html_node/Regular-Expressions.html

The first sentence in the above post should have read "b * d" but do not use the spaces. For some readon the editor removed the * in the sentence.

Thank you for your reply but the only library that i can use is stdio and string, so no cheating for me i'm afraid... i looked up the code for regexp and it looked like chinese to me, is there a simpler way of doing it?

This is what i've done so far:

int includes(char *pattern, char *line) {

    char *word;
    char *loc;
    int counter = 0;
    int i;
    int j = 0;

    if(strstr (pattern, DOT) != NULL) {

        /* 
        * Creates a array with the location of all the dots
        */

        loc = strchr(pattern,'.');

        while(loc != NULL) {
            loc = strchr(loc + 1,'.');
            counter++;
        }

        int dots [counter - 1];
        counter = 0;
        loc = strchr(pattern,'.');

        while(loc != NULL) {
            dots[counter] = loc-pattern;
            loc = strchr(loc + 1,'.');
            counter++;
        }
    }

    /*Returns 1 if the pattern is in the line, 0 if not */
    return 0
}

As you can see i have the location of all the dots and then i was thinking of perhaps splitting the line into words but that wouldn't work because if the line is say: blablablabtestebla and the pattern is t.st then it's still classed as true.

So again, thank you for your time but perhaps a simpler way?

The way I once did it was to just search each character in line for a match in pattern. Warning: I didn't compile this so it might contain bugs, but it should give you the general idea

int includes(char *pattern, char *line) {
   int good = 0; // assume not ok

   int i = 0,j  = 0;
   while(pattern[i])
   {
       if( line[j] == pattern[i])
       {
           ++j;
       }
       else if( pattern[i] == DOT)
       {
          ++i;
          if( pattern[i] ) {
              while( line[j] && line[j] != pattern[i])
                 ++j;
          }
          else
             return 1; // line has pattern
          if( line[j] == '\0')
             return 1; // line has pattern
       }
   }
}

I'm feeling so stupid right now... turns out i was overcomplicating it waayy too much, here's my solution:

    int includes(char *pattern, char *line) {

    int i = 0;
    int j = 0;




        while(line[i] != 0) {
            if((pattern[j] == DOT) | (pattern[j] == line [i])) {
                if(j == (strlen(pattern) - 1))
                    return 1;
                i++;
                j++;
            }
            else {
                j = 0;
                i++;
            }
        }


    return 0;
}

// About the google.com and the google2com situation, i was thinking of comparing pattern[j] to \ and then pattern[j+1] to . but how do i get the compiler to identify the \ as a \ and not an oporator? So that for example if the pattern is google\.com it won't print google2com. (Putting this as code was the only way to get the editor to recognize the \)
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.