# Looking for chars in string.

• May 20th, 2013, 10:10 AM
juanpast
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 :o

Best regards
• May 20th, 2013, 11:21 AM
GCDEF
Re: Looking for chars in string.
Your question isn't clear to me.
• May 20th, 2013, 12:29 PM
2kaud
Re: Looking for chars in string.
Quote:

Originally Posted by GCDEF
Your question isn't clear to me.

Nor to me either. I don't understand your example result numbers.
• May 20th, 2013, 02:15 PM
ninja9578
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?
• May 20th, 2013, 02:41 PM
2kaud
Re: Looking for chars in string.
Quote:

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