Mohanapriya R Mohanapriya R
Updated date Feb 05, 2024
In this article, we will learn the ROT13 encoding as we guide you through decoding secrets using simple C# programming.

Understanding ROT13 Encoding:

Before diving into the code, let's have a quick look at how ROT13 encoding works. The process involves shifting each letter by 13 positions in the alphabet, wrapping around if needed. For example, 'A' becomes 'N', 'B' becomes 'O', and so on. This symmetric nature of the transformation makes it easy to revert the process by applying ROT13 a second time.

Below is a simple C# program that takes a ROT13 encoded string as input and converts it back to its original form.

using System;

class Program
{
    static string DecodeRot13(string input)
    {
        char[] charArray = input.ToCharArray();

        for (int i = 0; i < charArray.Length; i++)
        {
            if (char.IsLetter(charArray[i]))
            {
                char start = char.IsUpper(charArray[i]) ? 'A' : 'a';
                charArray[i] = (char)(start + (charArray[i] - start + 13) % 26);
            }
        }

        return new string(charArray);
    }

    static void Main()
    {
        Console.Write("Enter a ROT13 encoded string: ");
        string encodedString = Console.ReadLine();

        string decodedString = DecodeRot13(encodedString);

        Console.WriteLine("Decoded String: " + decodedString);
    }
}

The DecodeRot13 function takes a ROT13 encoded string as input and iterates through each character. If the character is a letter, it applies the ROT13 decoding by shifting it appropriately. The Main method prompts the user to input a ROT13 encoded string, decodes it, and then prints the result.

Output:

Enter a ROT13 encoded string: Uryyb, gur jbeyq!
Decoded String: Hello, the world!

Comments (0)

There are no comments. Be the first to comment!!!