【问题标题】:Get special characters and numbers in output file? Using ROT 13在输出文件中获取特殊字符和数字?使用 ROT 13
【发布时间】:2019-10-28 04:50:30
【问题描述】:

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

【问题讨论】:

  • 你有一个空格的特殊条件 - 也许你应该改变它以适用于不在 alphabet 中的任何字符。

标签: c# decode encode rot13


【解决方案1】:

一种方法是创建一个变量,如果在alphabet 数组中找到该字符,我们将其设置为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;
    }
}

【讨论】:

    【解决方案2】:

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

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多