| < | January 2009 | |||||||
|---|---|---|---|---|---|---|---|---|
| S | M | T | W | T | F | S | ||
| 1 | 2 | 3 | ||||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 | ||
| 11 | 12 | 13 | 14 | 15 | 16 | 17 | ||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 | ||
| 25 | 26 | 27 | 28 | 29 | 30 | 31 | ||
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.
posted at: 06:03 | path: /Programming/OPB/Snippets | Permanent link to this entry | Add/View comments (0 existing)
Copyright ©2004-2009 O. Patrick Barnes. All rights reserved.