### look through a string of vectors and change letters

hi i need to look through a string of letters and change the letters simultaneously. so if i have aabbccttcc and do a change to c and c change to a. what happens is that first a change to c and that c changes back to a.

 ``12`` `````` replace (dna[0].begin(),dna[0].end(), 'a','c' ); replace (dna[0].begin(),dna[0].end(), 'c','a' );``````
I don't really understand what you wanted, but it seems as if you want to change every char that is 'a' into 'c' and every char that is 'c' into 'a'?

Well replace or replace_if would iterate once, so doing 2 replaces will not suffice because you'd end up with no 'c' in the string. Here's what I think should answer your request.

 ``123456789`` `````` std::string word("aabbccttcc"); for (std::string::iterator it = word.begin(); it != word.end(); ++it) { if (*it == 'a') *it = 'c'; else if (*it == 'c') *it = 'a'; } std::cout << word;``````
 ``123`` ``````std::transform(x.begin(), x.end(), x.begin(), [](char c){if(c == 'a') return 'c'; else if (c == 'c') return 'a'; return c;});``````
 ``12345678910111213`` ``````const std::string replace( const unsigned char oldCh, const std::string string , const unsigned char newCh ) { std::string newString = string; for ( unsigned int i = 0; i < string.size(); i++ ) { if( string[i] == oldCh) { newString[i] = newCh; } } return( newString ); }``````
thanks guys i totally forgot about the iterator
Topic archived. No new replies allowed.