Mohanapriya R Mohanapriya R
Updated date Nov 10, 2024
This article shows how to convert strings to ROT13 encode in C#.

Convert Strings to ROT13 Encode in C#

Below C# program shows how to convert strings to ROT13 encode in C#.

using System;

class Program
{
    static string EncodeRot13(string input)
    {
        char[] chars = input.ToCharArray();

        for (int i = 0; i < chars.Length; i++)
        {
            char c = chars[i];

            if (char.IsLetter(c))
            {
                char offset = char.IsUpper(c) ? 'A' : 'a';
                chars[i] = (char)((c - offset + 13) % 26 + offset);
            }
        }

        return new string(chars);
    }

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

        string encoded = EncodeRot13(input);

        Console.WriteLine($"Original: {input}");
        Console.WriteLine($"ROT13 Encoded: {encoded}");
    }
}

The EncodeRot13 method gets a string input from the user and iterates through each character. If the character is a letter, it applies the ROT13 transformation, considering the case of the letter. The Main method prompts the user for input, applies the ROT13 encoding, and displays both the original and encoded strings as output as shown below.

Output:

Enter a string: Hello, World!
Original: Hello, World!
ROT13 Encoded: Uryyb, Jbeyq!

Comments (0)

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