EDIT: Nevermind...
Realized my mistake.
You can delete this.
EDIT: Nevermind...
Realized my mistake.
You can delete this.
Last edited by Programmer_P; 06-02-2010 at 01:36 PM.
Well, its not really all that important.
Turns out the function wasn't returning the wrong values at all, like I thought it was.
I just forgot to output the right messages...
I meant to do this:
But instead, I ended up doing:Code:if (containsString) { cout << '\"' << sourceString << " contains \"" << searchString << '\"' <<end; } else { cout << '\"' << sourceString << " does NOT contain \"" << searchString << '\"' <<end; }
by accident, thus outputting the same message at either statement. But, in case anyone wants to view the now-working code, here is the link:Code:if (containsString) { cout << '\"' << sourceString << " contains \"" << searchString << '\"' <<end; } else { cout << '\"' << sourceString << " contains \"" << searchString << '\"' <<end; }
C++ code - 279 lines - codepad
Last edited by Programmer_P; 06-02-2010 at 01:53 PM.
Hmm...on second thought, looks like the "if (searchStringSize < sourceStringSize)" block is returning the wrong value from the containsString() function if sourceString doesn't contain searchString. It will return true regardless...
http://codepad.org/njKA37LE
I'll have to focus on that part of the code and fix whatever problem is there in my logic.
Last edited by Programmer_P; 06-02-2010 at 01:55 PM.
What am I missing in my logic?
Obviously I need another "return false" line in there somewhere, but I have no idea where to put it.Code:int currentIndexOfSearchString = 0; for (int i = 0; i < sourceStringSize; i++) { if (sourceString[i] == searchString[0]) { //then we have now found the first character of the searchString in the sourceString for (int i2 = i; i2 < sourceStringSize && i2 < searchStringSize; i2++) { if (sourceString[i2] != searchString[currentIndexOfSearchString]) { if (i2 == searchStringSize - 1) { //we're at the last character of the searchString return false; } else { if (i2 < sourceStringSize - searchStringSize) { //meaning we have enough characters in sourceString to store a possible match of searchString i = i2; //set the outer for loop iterator to the value of the inner one break; //out of this loop at the first char of the sourceString that doesn't match the current char of the searchString } else { return false; } } } else { //we're good so far... } currentIndexOfSearchString++; //increment to the next character index in the searchString } } }
Current source code:
C++ code - 284 lines - codepad
Are you trying to write a version of strstr() for practice or such? If not, there are functions you can use to make your life simpler if you are using the string class.
Functions such as what?
I don't know of any function in the string class for checking to see if one string contains another string (there is string::find(), but that returns the position of the string in the string, and I just want to know if one string contains another string). That's why I'm writing my own.
Like I mentioned, strstr() will find a string within another string.
O_othere is string::find(), but that returns the position of the string in the string, and I just want to know if one string contains another string
If you know this, and this isn't a learning exercise, why on earth aren't you using `std::string::find' to implement this?
Soma
Why a bool? Can you not convert the logic of "if this is null, it's false, otherwise it's true" yourself?
Good question.
And the answer is....
Because I didn't think of it.
Thanks for that though. It works now.
And the working version can be found at:
C++ code - 265 lines - codepad
EDIT: And for the record, this is a learning exercise...
I'm writing a program to convert enum values to strings as both a learning exercise, and because I need a program to do such a thing for another program I'm currently working on.
Last edited by Programmer_P; 06-02-2010 at 05:29 PM.
So this:
is not as good as this:Code:void somefunc() { bool containsString = str.find("awesome") != std::string::npos; }
Code:bool ContainsString(string x, string y) { //Insert bunch of custom code here return contains; } void somefunc() { bool containsString = ContainsString(str, "awesome"); }
Woop?