-
May 20th, 2013, 10:10 AM
#1
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
-
May 20th, 2013, 11:21 AM
#2
Re: Looking for chars in string.
Your question isn't clear to me.
-
May 20th, 2013, 12:29 PM
#3
Re: Looking for chars in string.
Originally Posted by GCDEF
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)
-
May 20th, 2013, 02:15 PM
#4
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
#5
Re: Looking for chars in string.
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|