Skip to content

Instantly share code, notes, and snippets.

@unilecs

unilecs/ROT13.cs Secret

Created June 6, 2019 06:12
Show Gist options
  • Save unilecs/53fb3459c96c3e09b4f0ae2f52cb7874 to your computer and use it in GitHub Desktop.
Save unilecs/53fb3459c96c3e09b4f0ae2f52cb7874 to your computer and use it in GitHub Desktop.
Задача: ROT13
using System;
using System.Text;
public class Program
{
private static string ROT13(string str)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
int code = (int)str[i];
if (code >= 'a' && code <= 'm' || code >= 'A' && code <= 'M')
{
code += 13;
}
else if (code >= 'n' && code <= 'z' || code >= 'N' && code <= 'Z')
{
code -= 13;
}
result.Append((char)code);
}
return result.ToString();
}
private static void Test_ROT13(string str)
{
Console.WriteLine(string.Format("Input string = {0}", str));
string codedMsg = ROT13(str);
Console.WriteLine(string.Format("Coded string = {0}", codedMsg));
string decodedMsg = ROT13(codedMsg); // should be equal to the original message
Console.WriteLine(string.Format("Decoded string = {0}", decodedMsg));
Console.WriteLine(string.Format("Is valid = {0}\n\n", str.Equals(decodedMsg)));
}
public static void Main()
{
Console.WriteLine("UniLecs");
Test_ROT13("Subscribe to UniLecs!");
Test_ROT13("Make Russia Great Again!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment