Recommended Posts

char string1[] = "Programmming";
char string2[] = "gram";
how would i find where string2 is inside string1?
i know i could use the find() function but i need to write my own function to do it and i''m stumped!!
can somebody please help?
thanks
NiNo

Share this post

Link to post

Share on other sites

Guest Anonymous Poster

Guest Anonymous Poster

There are several ways of doing this. The first approach is the one detailed by JY, and is a brute force approach. A more efficient approach would be to use the Knuth-Morris-Prath or Boyer-Moore algormithms. Both of these depends on first examining the pattern (the string you are looking for) for patterns. For natural language texts (e.g. english) BM is usually significantly more efficient than both other methods.

Without going in to too much detail on how this algorithm works it basically looks at the character in the text on which a mismatch occurs, and moves the pattern forward until the last occurence of that character in the pattern is lined up with it.Oh, and it also checks from the end of the pattern backwards, rather than from the start of the pattern forwards.

Maybe a small example would help a little.Assuming the text-string you are searcing in isI REALLY LOVE DEBUGGING MY CODEand the string you are searching for isMY CODE

Starting at the end of the pattern we get a mismatch on L (in the text).Since skip[''L''] is 7 we move the pattern 7 positions to the right, thus gettingI REALLY LOVE DEBUGGING MY CODE MY CODEAt this point we get a mismatch on '' '' (in the text).Since skip['' ''] is 4 we move the pattern 4 positions, gettingI REALLY LOVE DEBUGGING MY CODE MY CODENote that this skip moved the last occurence of '' '' in the pattern to match the '' '' we just found in the text. This is really the meat and bones of the algorithm, as whenever we find a character in the text we know we have in the pattern, we move the pattern so that that character matches in the pattern, and if we find a character in the text that isn''t in the pattern we move the pattern just past that point in the text.

Anyway, that position above gives us a mismatch on ''U'' which doesn''t exist in our pattern, so again we move 7 positions gettingI REALLY LOVE DEBUGGING MY CODE MY CODE

skip[''M''] is 6, so we move 6 spaces and getI REALLY LOVE DEBUGGING MY CODE MY CODEAnd we''re done.This algorithm is clearly a lot faster than a simple brute-force approach as the pattern is literally flying along often moving 7 spaces at the time without comparisons, while the brute-force approach would never move more than 1 before comparing again.