Comments about your code:
I believe that you would rename it to something like
strstr if you could.
You should accept pointers to
const char, since you don't modify them.
You should initialize
n right inside the loop. It will narrow their scope and make the code more maintainable since it won't pollute the scope of the function. On top of that, they will be deallocated whenever the loop ends.
int for index is a very bad idea. The biggest issues with it that it is signed and not guaranteed to be large enough. You should use
size_t instead, because it is guaranteed that
size_t will have enough capacity to index array of maximum possible size.
You're increasing overhead by using subscript (
Loop could be rewritten in other, more readable and straightforward form, one of which is shown in suggested implementation. I know that you would like to have high performance, but I don't think that it is that important in this situation. May be with more context of the usage of the algorithm I could suggest particular algorithm (there are quite a few of them). The best in my opinion is Boyer–Moore string search algorithm.
I think that
strncmp should be used. I know that including
string.h header defeats the purpose, but you could write your own. It increases code reuse in C!
To make it work we just need to precalculate length of the substring.
const char *strs(const char *text, const char *substr)
size_t substrlen = strlen(substr);
while (*text != 0)
if (strncmp(text, substr, substrlen) == 0)
The performance gain from the data being const depends on physical constness and the compiler. Further reading.
About performance gain..