C# 是否在输出文件中获取特殊字符和数字?使用ROT 13
该程序成功地使用了ROT-13算法,但它没有捕获任何特殊字符或数字,相反,特殊字符和数字根本没有保存在输出文件中。对于要输出的数字和特殊字符,我需要在代码中做哪些更改C# 是否在输出文件中获取特殊字符和数字?使用ROT 13,c#,decode,encode,rot13,C#,Decode,Encode,Rot13,该程序成功地使用了ROT-13算法,但它没有捕获任何特殊字符或数字,相反,特殊字符和数字根本没有保存在输出文件中。对于要输出的数字和特殊字符,我需要在代码中做哪些更改 static void Encode () { string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // declaring alphabet string strROT13 = "no
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.");
}
静态无效编码()
{
string alphabet=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz”//声明字母表
string strort13=“nopqrstuvxyzabcdefghijklmnopqrstuvxyzabcdefghijklm”//使用ROT13系统声明字母表
Console.WriteLine(“输入要编码的文件名(.txt、.bat等):”;//提示用户读入文件
字符串inputFile=Console.ReadLine();
如果(!File.Exists(inputFile)){//测试是否找到文件
WriteLine(“未找到文件,请重试。”);
返回;
}
Console.WriteLine(“输入要保存到(.txt、.bat等)的文件名):”;//询问用户将翻译后的文件保存到何处
字符串outputFile=Console.ReadLine();
StreamReader输入=新建StreamReader(inputFile);//读取文件
StreamWriter输出=新建StreamWriter(outputFile);//写入文件
string str=”“;//逐行读取文件
而((str=input.ReadLine())!=null)//读取整个文件
{
字符串编码=”;
int长度=str.长度;
如果(长度>0)
{
foreach(str中的字符)//从行中提取每个字符
{
if(character='')//如果文件中有空格,则保持不变
编码+='';
其他的
for(int i=0;i<52;i++)//如果数组中有字符,则进行编码
如果(字符==字母表[i])
编码+=strot13[i];
}
}
output.WriteLine(encoded);//将编码字符串写入新文件
}
input.Close();
output.Close();
WriteLine(“文件已成功编码”);
}
一种方法是创建一个变量,如果在字母表
数组中找到字符,则将该变量设置为true
。然后,在内部循环之后,我们知道如果没有找到该字符,它就是一个“特殊字符”或一个数字,我们可以添加它
例如:
foreach (char character in str) // takes each character from the line
{
bool characterFound = false;
for (int i = 0; i < alphabet.Length; i++) // if character in array, then encoded
{
if (character == alphabet[i])
{
encoded += strROT13[i]; // add the encoded character
characterFound = true; // set our variable to indicate it was found
break; // break out of the for loop early
} // since there's no need to keep searching
}
// If this character was not found in alphabet, just add it as-is
if (!characterFound)
{
encoded += character;
}
}
foreach(str中的字符)//从行中提取每个字符
{
bool characterFound=false;
对于(int i=0;i
我认为您应该对字符串中未包含的所有字符进行常规检查,然后手动将它们附加到编码字符串中。试试这个:
foreach (char character in str) // takes each character from the line
{
if (alphabet.ToCharArray().Contains(character))
{
encoded += strROT13[Array.IndexOf(alphabet.ToCharArray(), character)];
}
else
{
encoded += character;
}
}
对于空格,您有一个特殊的条件-也许您应该将其更改为适用于
字母表中不存在的任何字符。