views:

127

answers:

2

I'm writing a grep function in C++ (as a self assigned exercise - I realize this doesn't have the functionality of an actual grep feature) to take an original string and the search string you are looking for. In the code, I enter all the characters in a grep string up to the first space it sees. Then I compare the characters in the grep string with the search string, and if it matches, store it in a temp string. I loop over the grep string and compare the length of the search string with the temp string to see if it matches.

My question: is that bad form, comparing lengths? I could use a for loop to compare each individual character against each other, but that seems like it would eat CPU cycles unneccessarily. Here is my enter function for reference:

std::string grep(std::string originalStr, std::string searchStr)
{
std::string grepStr = "";
std::string finalStr = "";
//stores final string; is the return value
finalStr.resize(originalStr.length() + 1);
grepStr.resize(originalStr.length() + 1);

int place = 0;
//remember where you are in originalStr[place]
int numOfOccurences = 0;
//remember number of times searchStr was found;
//not necessary
std::string tempStr = "";
//will temporarily hold grepStr    

//handles case if first occurence is a space
if (originalStr[0] == ' ')
{
    place++;
}

while (place != originalStr.length())
{
    tempStr = "";

    while (originalStr[place] != ' ')
    {

        if (originalStr[place] == ' ')
        {
           break;
        }

        grepStr[place] = originalStr[place];
        ++place;
    }

    ++place;//ensures you skip over the space next pass

    for (int i = 0; i != grepStr.length(); i++)
    {
        if (grepStr[i] == searchStr[i])
        {
            //if they are the same, append that char..
            tempStr[i] = grepStr[i];

            if (tempStr.length() == grepStr.length())
            //..then check for string length; if same, searchStr equals tempStr
            //and you can append grepStr to the finalStr
            {                    
                for (int x = 0; x != finalStr.length(); x++)
                {
                    finalStr[x] = grepStr[x];
                }

                ++numOfOccurences;
                //add one to the number of occurences in originalStr
                finalStr += ' ';
                //add a space IF you find the string
            }
        }
    }
}

return finalStr;
}
+4  A: 

Nope, not bad form at all. After all, at least in some senses, two strings can't be equal if they're not of equal length.

For a really good time, have a look at the Boyer-Moore string matching algorithm and the Knuth-Pratt-Morris algorithm. J [sic! He really spells it that way] Moore has a nice page on them.

Charlie Martin
I found the link about Boyer-Moore Fast String Searching Algorithm very interesting, thank you!
Daniel Persson
Glad you like it. It really is a kind of cool algorithm; really pretty counterintuitive that you can do string matching that fast.
Charlie Martin
A: 

If you are using std::string, the STL find functions will probably run more efficiently than this version you've created. Searching a string for a substring is a known problem and I'm sure the library version is as optimized as it can be.

jmucchiello
You're no fun. ;-)
Charlie Martin
No fun? Hey, if he wants to reinvent the wheel for his own personal edification, I'm not going to stop him. But I don't want to maintain his "Wheel 2.0.0alpha" if I end up working where he use to work. That's no fun.
jmucchiello
Sorry, I'm just learning to program now with C++. I am unfamiliar with the find function but will try and use it in the future.
Hooked