Occurences of a string in another string.

This is a discussion on Occurences of a string in another string. within the C++ Programming forums, part of the General Programming Boards category; Sorry first line...it should say i==2, not x==2....

  1. #16
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Sorry first line...it should say i==2, not x==2.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    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.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    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.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    They aren't templates. It's the type strlen returns.
    strlen (MSDN)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    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?

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    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.

  10. #25
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    theres an O(n) solution, KMP algo

  11. #26
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by rodrigorules View Post
    theres an O(n) solution, KMP algo
    Excellent....thanks, will have a read up on that.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21