My program shows the letter and then asks with what letter should you replace it:
static void Main(string[] args)
{
string textToEncode = File.ReadAllText(@"C:\Users\ASUS\Desktop\szyfrowanie2\TextSample.txt");
textToEncode = textToEncode.ToLower();
string distinctLetters = new string(textToEncode.Distinct().ToArray());
var count = textToEncode.Distinct().Count();
Console.WriteLine("Letters used in text: \n\n");
int iteration = 0;
for (int i = 0; i < count; i++)
{
if (Equals(distinctLetters[i], ' ')) { Console.Write(" <space> "); }
else if (Equals(distinctLetters[i], '\r')) { continue; }
else if (Equals(distinctLetters[i], '\n')) { continue; }
else { Console.Write(distinctLetters[i] + " "); }
}
Console.WriteLine("\n\nEncoding: \nPlease do not use single non-letter characters for coding (such as ',' or '?'");
List<string> charsUsedToEncode = new List<string>();
List<string> charsEncoded = new List<string>();
while (iteration < count)
{
if (Equals(distinctLetters[iteration], ' ')) { Console.Write("Swap <space> with "); }
else { Console.Write("Swap " + distinctLetters[iteration] + " with "); }
string string1 = Console.ReadLine();
if (string.IsNullOrEmpty(string1) == true)
{
Console.WriteLine("You have to type a character. ");
}
else if (string1.Length > 1)
{
Console.WriteLine("You entered more than one character. ");
}
else
{
char SwappingMark = char.Parse(string1);
if (charsUsedToEncode.Contains(SwappingMark.ToString()))
{
Console.WriteLine("\nThis character has already been used.");
}
else if (Equals(distinctLetters[iteration], '\r')) { continue; }
else if (Equals(distinctLetters[iteration], '\n')) { continue; }
else
{
charsEncoded.Add(distinctLetters[iteration].ToString());
charsUsedToEncode.Add(SwappingMark.ToString());
SwappingMark = char.ToUpper(SwappingMark);
textToEncode = textToEncode.Replace(distinctLetters[iteration], SwappingMark);
iteration++;
}
}
}
textToEncode = textToEncode.ToLower();
Console.ReadLine();
}
The problem is, after a '.' char The word "Swap" disappears. It looks like this:
Swap w with a
...
Swap . with x
y with l
The word "Swap" disappears. In my TextSample after a '.' text starts in new line and I don't know why, since there is a else if (Equals(distinctLetters[iteration], '\n')) { continue; }
condition.
As I mentioned in the comments, the problem is that you skip the new-line characters before printing them but they're still in the string. So, you skip them again but after actually printing the "swap" statement to the user (causing those characters to be displayed before they're ignored).
What you should do instead is not include them in the array from the beginning. Also, there's no need to create a new string; you can just use a char array like this:
char[] distinctLetters = textToEncode.Distinct()
.Where(c=> !"\r\n".Contains(c))
.ToArray();
That way, you never have to check for \r
or \n
again since they don't exist.
Full example:
static void Main(string[] args)
{
string textToEncode = File.ReadAllText(@"C:\Users\ASUS\Desktop\szyfrowanie2\TextSample.txt");
textToEncode = textToEncode.ToLower();
char[] distinctLetters = textToEncode.Distinct()
.Where(c=> !"\r\n".Contains(c))
.ToArray();
var count = distinctLetters.Count();
Console.WriteLine("Letters used in text: \n\n");
int iteration = 0;
for (int i = 0; i < count; i++)
{
if (distinctLetters[i] == ' ') { Console.Write(" <space> "); }
else { Console.Write(distinctLetters[i] + " "); }
}
Console.WriteLine("\n\nEncoding: \nPlease do not use single non-letter characters for coding (such as ',' or '?'");
List<string> charsUsedToEncode = new List<string>();
List<string> charsEncoded = new List<string>();
while (iteration < count)
{
if (distinctLetters[iteration] == ' ') { Console.Write("Swap <space> with "); }
else { Console.Write("Swap " + distinctLetters[iteration] + " with "); }
string string1 = Console.ReadLine();
if (string.IsNullOrEmpty(string1))
{
Console.WriteLine("You have to type a character. ");
}
else if (string1.Length > 1)
{
Console.WriteLine("You entered more than one character. ");
}
else
{
char SwappingMark = char.Parse(string1);
if (charsUsedToEncode.Contains(SwappingMark.ToString()))
{
Console.WriteLine("\nThis character has already been used.");
}
else
{
charsEncoded.Add(distinctLetters[iteration].ToString());
charsUsedToEncode.Add(SwappingMark.ToString());
SwappingMark = char.ToUpper(SwappingMark);
textToEncode = textToEncode.Replace(distinctLetters[iteration], SwappingMark);
iteration++;
}
}
}
textToEncode = textToEncode.ToLower();
Console.ReadLine();
}