How do I check if a string is a substring of another string in C without using the substr function?
You could use the rolling hash method (Rabin-Karp), or implement KMP, which is a bit like a state machine.
Generally, Rabin-Karp is used when you are searching for multiple target strings, because the second check it requires (for actual equality rather than just hash equality) is well worth it to be able to examine the source string only once. In your case, either will do just fine.
There is also a more naive solution which is O(n^2) and requires checking for the target string at every position in the source string.
Don't use substr
… use strstr
/strnstr
!
Seriously, nothing else will be as fast unless it does exactly the same thing. Why don't you want that?
Edit: yeah, there are algorithms with better asymptotic performance. For text-like searches for a single string, I'd be pretty surprised to see anything outperform a standard library, though.