Looking for chars in string.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Looking for chars in string.

  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
    Posts
    12,093

    Re: Looking for chars in string.

    Your question isn't clear to me.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,370

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,370

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center