i'd appreciate if someone could help! I need to replace each character in my text(encrypted,which i read from file) with another character, which i have in my Dictionary.
StreamReader st = new StreamReader(@"C:\path of text");
string text = st.ReadToEnd();
st.Close();
char[] textChar = text.ToCharArray(); //splitting text into characters
So, in my Dictionary Dictionary<char, char> keys = new Dictionary<char,char>();
for key i have some letter, say 'n' and for value - another letter, say 'a'. So i need to replace each 'n' with 'a' in my text. Dictionary has 26 letters for keys and 26 letters for values respectively.
Now i try to replace letters and write 'decrypted' text into some file
StreamWriter sw = new StreamWriter(@"path for decrypted file");
foreach(KeyValuePair<char, char> c in keys)
{
for(int i =0; i< textChar.Length; i++)
{
if (textChar.Contains(c.Key))
{ //if text has char as a Key in Dictionary
textChar[i] = keys[c.Key]; //replace with its value
}
else
{
sw.Write(textChar[i]); //if not, just write (in case of punctuatuons in text which i dont want to replace)
}
}
}
st.Close();
file.Close();
This code does not work correctly, because the replacement is wrong. I'd be so grateful for any help!