Sorry first line...it should say i==2, not x==2.
Sorry first line...it should say i==2, not x==2.
I think I see your twisted sense of logic now. It's undoubtedly different from how I would do, and that is why I was a little thrown off.
I just made a little remark about the subtraction. I didn't mean it was wrong, because I wasn't sure and I didn't test.
Out of curiosity, how would you do it? I'm always interested to see how other people would tackle the same problem?
I do think the -1 was incorrect, but somehow it seemed to work? Not sure how tbh.
Patience. After I wrote the reply, I started working on my own version. And a few minutes later, here it is:
(Not tested.)Code:bool IsStrInStr(char* src, char* find, size_t length_of_src, size_t length_of_find) { if (length_of_src < length_of_find) return false; for (int i = 0; i < length_of_src; ++i) { if (src[i] != find[i]) return false; } return true; } size_t findx(char* src, char* find) { size_t len_src = strlen(src); size_t len_find = strlen(find); int found = 0; for (int i = 0; i < len_src - len_find; ++i) { if (IsStrInStr(&src[i], find, len_src - i, len_find) found++; } return found; }
They aren't templates. It's the type strlen returns.
strlen (MSDN)
No, all variables are finite since there's finite storage. But strlen returns a size_t. And if you use a smaller data type, you may lose data. So by using size_t, you are guaranteed not to lose any information. That's all.
Keep the type chain.
(Plus the fact that a length cannot be negative; so assigning it to an int may cause signed issues.)
theres an O(n) solution, KMP algo