CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Looking for chars in string.

    Hi gurus.

    I'm looking for a algorithm to search portions of string that have the same caracter. The only possible values are: a,n and g

    Char index: 0 1 2 3 4 5 6 7 8 9 RESULT
    ------------------------------------------
    Example 1: a g g g a a 0,4
    Example 2: g g g a n n a 0,3
    Example 3: a g g g g g g a 0,7
    Example 4: g g g g g g g 0,6
    Example 5: g g a a g a a a g 0,2,3,5,7,8


    Thanks in advance, I'm collapsed

    Best regards

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Looking for chars in string.

    Your question isn't clear to me.

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Looking for chars in string.

    Quote Originally Posted by GCDEF View Post
    Your question isn't clear to me.
    Nor to me either. I don't understand your example result numbers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Looking for chars in string.

    You are looking for the index where the string changes from one char to another?

    Code:
    std::string mystring = "ggaagaaag";
    size_t index = 0;
    char last = '\0';
    for(std::string::iterator i = mystring.begin(); i < mystring.end(); ++i, ++index){
        if (*i != last) std::cout << index;
        last = *i;
    }
    std::cout << index;
    Is that what you are looking for?

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Looking for chars in string.

    Quote Originally Posted by ninja9578 View Post
    You are looking for the index where the string changes from one char to another?

    Code:
    std::string mystring = "ggaagaaag";
    size_t index = 0;
    char last = '\0';
    for(std::string::iterator i = mystring.begin(); i < mystring.end(); ++i, ++index){
        if (*i != last) std::cout << index;
        last = *i;
    }
    std::cout << index;
    Is that what you are looking for?
    That's what I originally thought was required, but the output from this doesn't match the results of the given examples.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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