0

该程序成功地使用了 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.");
    }
4

2 回答 2

0

我认为您应该对字符串中未包含的所有字符进行一般检查,然后将它们手动附加到编码字符串中。尝试这个:

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;
    }
}
于 2019-10-28T05:31:59.497 回答
0

一种方法是创建一个变量,true如果在alphabet数组中找到字符,我们将设置为该变量。然后,在内循环之后,我们知道如果没有找到字符,它是一个“特殊字符”或一个数字,我们可以添加它。

例如:

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;
    }
}
于 2019-10-28T05:07:49.673 回答