# Occurences of a string in another string.

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 07-27-2010
darren78
Sorry first line...it should say i==2, not x==2.
• 07-27-2010
Elysia
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.
• 07-27-2010
darren78
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.
• 07-27-2010
Elysia
Patience. After I wrote the reply, I started working on my own version. And a few minutes later, here it is:
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; }```
(Not tested.)
• 07-27-2010
darren78
Quote:

Originally Posted by Elysia
Patience. After I wrote the reply, I started working on my own version. And a few minutes later, here it is:
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; }```
(Not tested.)

Thanks. Are the size_t templates? I've not covered them yet. I'll make a note of this code and come back once I understand templates.
• 07-27-2010
Elysia
They aren't templates. It's the type strlen returns.
strlen (MSDN)
• 07-27-2010
darren78
Quote:

Originally Posted by Elysia
They aren't templates. It's the type strlen returns.
strlen (MSDN)

Does using size_t have benefits over using, for example, int?

Is it like size_type where is it guaranteed to hold any number, whereby int would wrap around once the value gets too high?
• 07-27-2010
Elysia
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.)
• 07-27-2010
darren78
Quote:

Originally Posted by Elysia
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.)

Okay cheers again.
• 07-27-2010
rodrigorules
theres an O(n) solution, KMP algo
• 07-27-2010
darren78
Quote:

Originally Posted by rodrigorules
theres an O(n) solution, KMP algo

Excellent....thanks, will have a read up on that.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12