Recently I came across a situation where I needed to search for the first occurrence of any one of a number of sub-strings inside a rather large string. Using strstr would have been quite inefficient because the string would have been searched from it's beginning for each sub-string until one was found. Hence I wrote MultiStrFind. This is a C++ function* that takes the string to be searched and an array of sub-strings to search for within the string as parameters. A pointer to the first occurrence of any one of the sub-strings will be returned.
Some improvements can still be made. For instance, I'm considering performing a binary search through the sub-string array instead of looping through the sub-string array once a character has been found that is the first character of any sub-string.
You can download the code snippet from here: MultiStrFind.zip
* And probably C too, but come to think of it I haven't tried to compile it with a C compiler.
Copyright ©2004-2008 O. Patrick Barnes. All rights reserved.