Letter replacement in string
c#
Solution
Just iterate through letters and change them one by one:
foreach(char c in key){
if(c==letter1){
newKey.Append(letter2);
}else if(c==letter2){
newKey.Append(letter1);
}else{
newKey.Append(c);
}
}
Problem
I have this simple code for letter substitution. What I would like to add is, that if i.e., I replace letter A with letter T, all T letters are automatically replaced with A as well. So if I have a word "atatatat", the following code changes the word to "tttttttt", but it should change it to "tatatata". How can I fix this? ``` private void button3_Click(object sender, EventArgs e) { String key= this.textBox1.Text; String letter1 = this.textBox2.Text; String letter2 = this.textBox3.Text; StringBuilder newKey = new StringBuilder(); newKey.AppendLine(key); newKey.Replace(letter1, letter2); this.textBox4.Text = noviKljuc.ToString(); } ``` I tried with adding this line: `newKey.Replace(letter2, letter1);` But this changes word to "aaaaaaaa"