该程序成功地使用了 ROT-13 算法,但它没有捕获任何特殊字符或数字,而是根本没有将特殊字符和数字保存在输出文件中。我需要对我的代码进行哪些更改才能输出数字和特殊字符?
static void Encode ()
{
string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // declaring alphabet
string strROT13 = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; // declaring alphabet using the ROT13 system
Console.WriteLine("Enter the name of the file you would like to encode (.txt, .bat, etc): "); // prompts user to read in file
string inputFile = Console.ReadLine();
if (!File.Exists(inputFile)) { // test to see if file is found
Console.WriteLine("File not found, please try again.");
return;
}
Console.WriteLine("Enter the name of the file you would like to save to (.txt, .bat, etc): "); // asks user where to save translated file
string outputFile = Console.ReadLine();
StreamReader input = new StreamReader(inputFile); // reads file
StreamWriter output = new StreamWriter(outputFile); // writes file
string str = ""; // reading file line by line
while ((str = input.ReadLine()) != null) // reads entire file
{
string encoded = "";
int length = str.Length;
if (length > 0)
{
foreach (char character in str) // takes each character from the line
{
if (character == ' ') // if a space in file, left unchanged
encoded += ' ';
else
for (int i = 0; i < 52; i++) // if character in array, then encoded
if (character == alphabet[i])
encoded += strROT13[i];
}
}
output.WriteLine(encoded); // writes encoded string to the new file
}
input.Close();
output.Close();
Console.WriteLine("The file was successfully encoded.");
}