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;
}