using System;
using System.Text;
/*
ROT13 is a simple substitution cipher that replaces a letter with the 13th letter
after it in the alphabet. Because the English alphabet has 26 letters (13 * 2),
applying ROT13 twice restores the original text (it is its own inverse).
*/
class Program
{
/// <summary>
/// Applies the ROT13 cipher to the specified text.
/// ROT13 rotates each letter by 13 positions in the alphabet.
/// Non-alphabetic characters are left unchanged.
/// Applying ROT13 twice returns the original text.
/// </summary>
/// <param name="input">The text to encode or decode.</param>
/// <returns>The ROT13-transformed string.</returns>
public static string Rot13(string input)
{
if (input == null) {
throw new ArgumentNullException("input");
}
StringBuilder result = new StringBuilder(input.Length);
foreach (char ch in input)
{
if (ch >= 'A' && ch <= 'Z') {
// Rotate uppercase letters (A-Z).
result.Append((char)('A' + ((ch - 'A' + 13) % 26)));
}
else if (ch >= 'a' && ch <= 'z') {
// Rotate lowercase letters (a-z).
result.Append((char)('a' + ((ch - 'a' + 13) % 26)));
}
else {
// Leave digits, spaces, punctuation, etc. unchanged.
result.Append(ch);
}
}
return result.ToString();
}
static void Main()
{
string input = "Hello, World! 123";
string encoded = Rot13(input);
string decoded = Rot13(encoded);
Console.WriteLine("Input : " + input);
Console.WriteLine("Encoded : " + encoded);
Console.WriteLine("Decoded : " + decoded);
}
}
/*
run:
Input : Hello, World! 123
Encoded : Uryyb, Jbeyq! 123
Decoded : Hello, World! 123
*/