CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2005
    Posts
    28

    Searching a string

    I'd like to search for a short string in a long string
    but find() can only give me the position of the first meet, whereas I want to take out all of their matching positions. How can i do this in C and or or C++ ?
    Thank you

    Eample: input: There is a skeleton in his cupboard and a small gun in his shirt's pocket
    search for in, and return two matches
    These are bunnies, copy them into your sig to help blue bunny gain good ratings and support purple bunny to build bigger gaming systems.
    (\ /)
    (\./)
    (' ,')('. ')
    (><)(><)
    Both are mine

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Searching a string

    Just use std::find in a loop.

    Code:
    std::vector<std::string::size_type> find_all(const std::string &s, const std::string &tofind)
    {
      std::vector<std::string::size_type> result;
      std::string::size_type pos;
      pos = s.find(tofind);
      while (pos != std::string::npos) {
        result.push_back(pos);
        pos = s.find(tofind, pos + 1);
      }
      return result;
    }
    This constructs a vector of all the positions where the substring occurs.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    May 2005
    Posts
    28

    Re: Searching a string

    Thank you yves help me.
    Could yves tell what is differnt between
    do {
    /////////

    }while(condition)

    and
    while(condition){
    ////////
    }

    which one more effective ? if in thoes blocks have lot of codes, or are they the same ?
    my teacher says they are the same.
    Thank you
    These are bunnies, copy them into your sig to help blue bunny gain good ratings and support purple bunny to build bigger gaming systems.
    (\ /)
    (\./)
    (' ,')('. ')
    (><)(><)
    Both are mine

  4. #4
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: Searching a string

    The difference between the two loops you posted is that in a while loop, the loop condition is checked before the body of the loop is executed. Thus, when the while statement is first encountered, if the condition is false, the loop will not run at all. In the do...while loop, on the other hand, the condition is checked after the body of the loop executes, so the loop will always run at least once. For example:
    Code:
    int n = -1;
    while (n > 0)
    {
        // loop code here
    }
    In the above code snippet, the lines inside the loop will never be executed. Compare that to:
    Code:
    int n = -1;
    do
    {
        // loop code here
    } while (n > 0);
    Here the body of the loop will run exactly once, because the program doesn't check whether n > 0 until after the body of loop has run.

    There's no difference in performance between the two; which one you use will simply depend on the nature of your problem in each situation. If the possibility exists that your loop should not run at all, use the while loop. If you want the loop to run at least once no matter what happens, the do...while loop may be more appropriate.

  5. #5
    Join Date
    May 2005
    Posts
    28

    Wink Re: Searching a string

    I asked beacause i set a breakpoint and debug and see things differnt. thank you,

    btw, i see yves online but yves didn't anser, i guess he ansers original question only, nevr for my second question. again, thanks
    These are bunnies, copy them into your sig to help blue bunny gain good ratings and support purple bunny to build bigger gaming systems.
    (\ /)
    (\./)
    (' ,')('. ')
    (><)(><)
    Both are mine

  6. #6
    Join Date
    May 2005
    Posts
    47

    Re: Searching a string

    You saw it different because of your pieces of code constructs you have created.
    Smashers/Devourer explanations was correct about how you could obtain the results output from the two "while's" but I think another point that should be taken into consideration is about the optimization problem of code structure in which the pretested loop does not give the best performance or the speed to run a bunch of code if present in the 'while' block. This is because there needs to be an extra unconditional jump at the end of the loop block to return to the beginning of the loop whereas the posttested loops in fact needs no such a jump and they become more effecient.
    I guess so...

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Searching a string

    Quote Originally Posted by grabbler
    This is because there needs to be an extra unconditional jump at the end of the loop block to return to the beginning of the loop whereas the posttested loops in fact needs no such a jump and they become more effecient.
    I guess so...
    I don't think so ! I don't understand the mechanism. And I feel even though this happens to be the case it would be there in both the cases.

    I feel the only difference here is what has been already pointed out by Smasher/Devourer.

    Cheers,
    Exterminator

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured