today I've encountered a logic error while working on a school project. For this project, switching chars around is required.
Say for example, the user enters the letter A. it is switched to U, and all is well. However, the issue occurs when the user enters the letter U. The character remains untouched. I've implemented two rules to switch from A to U and U to A. The string passes through both of the rules, and therefore is unchanged. How can I prevent this?
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string seq;
getline(cin, seq);
transform ( seq.begin(), seq.end(), seq.begin(), ::toupper );
replace ( seq.begin(), seq.end(), 'A', 'U' );
replace ( seq.begin(), seq.end(), 'U', 'A' );
cout << seq;
return 0;
}
<algorithm.h>and not<algorithm>?Awith a character x that otherwise does not appear in the string thenUwith a second charater y and then replace x with U and y with A respectively<algorithm>ideone.com/ia9fJetransform. Think about how you can modify this.