Get special characters and numbers in output file? Using ROT 13

405 Views Asked by At

This program is using the ROT-13 algorithm successfully, but it is not capturing any special characters or numbers, instead, special characters and numbers are not being saved in the output file at all. What changes in my code do I need to make for numbers and special characters to output?

    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.");
    }
2

There are 2 best solutions below

0
Rufus L On

One way to do this is to create a variable that we set to true if the character was found in the alphabet array. Then, after the inner loop, we know that if the character wasn't found, it was a "special character" or a number, and we can just add it.

For example:

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;
    }
}
0
preciousbetine On

I think you should do a general check for all characters not contained in the string then append them manually to the encoded string. Try this:

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