-
-
Save unilecs/53fb3459c96c3e09b4f0ae2f52cb7874 to your computer and use it in GitHub Desktop.
Задача: ROT13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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