I have a WPF application which has 2 textboxes with one of them being filled with a ciphered text (from a txt file) and one of them being filled with a reference text (also from a txt file). I have to count the number of times each character of the alphabet repeats in both texts and just switch the characters, so the ciphered text would be deciphered.
Now I have to switch the characters where the number of repetitions in both texts are the same (or if it's the same number of repetitions, just take the first one) and apply that to the ciphered textbox (so when I would click on the decipher button, it would change the letters in the textbox).
Here is the code I have so far:
public partial class MainWindow : Window
{
public static SortedDictionary<char, int> Count(string stringToCount)
{
SortedDictionary<char, int> characterCount = new SortedDictionary<char, int>();
foreach (var character in stringToCount)
{
int counter = 0;
characterCount.TryGetValue(character, out counter);
characterCount[character] = counter + 1;
}
return characterCount;
}
public MainWindow()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs ex) //Decipher button
{
var ciphered = Count(textBox.Text);
var referenced = Count(textBox1.Text);
var deciphered= "";
foreach (var val in ciphered)
{
foreach (var x in referenced)
{
if (val.Value == x.Value)
{
deciphered = textBox.Text.Replace(val.Key.ToString(), x.Key.ToString());
}
}
}
textBox.Text = deciphered;
}
}
The above code "kind of" works, but I have to press the button multiple times, when it should be done in one click.
All help would be much appreciated!