This transformation cipher shifts letters 13 places in the alphabet. Generally, we can change the individual characters in a string
based on logic.
For this cipher, many implementations are possible. Some are faster than others. Some initialization time may be required. ROT13 helps us learn how to transform strings.
In a for
-loop, we test each character, and add it to a character buffer. We could use StringBuilder
, but this would be slower for single character appends.
char
lookup table method, which is faster, although somewhat more complex.static
class
. It calls the ToCharArray
method.Transform()
casts the char
to an integer and then transforms that value. It casts back to a char
, and uses the string
constructor.using System; class Program { static void Main() { string value = "The apartment is 700 square feet."; Console.WriteLine(value); value = Rot13.Transform(value); Console.WriteLine(value); value = Rot13.Transform(value); Console.WriteLine(value); } static class Rot13 { /// <summary> /// Performs the ROT13 character rotation. /// </summary> public static string Transform(string value) { char[] array = value.ToCharArray(); for (int i = 0; i < array.Length; i++) { int number = (int)array[i]; if (number >= 'a' && number <= 'z') { if (number > 'm') { number -= 13; } else { number += 13; } } else if (number >= 'A' && number <= 'Z') { if (number > 'M') { number -= 13; } else { number += 13; } } array[i] = (char)number; } return new string(array); } } }The apartment is 700 square feet. Gur ncnegzrag vf 700 fdhner srrg. The apartment is 700 square feet.
The ROT13 algorithm that uses a lookup table performs better but has a much higher initialization cost. It is ideal only for programs that transform huge amounts of text.
if
-branch method where each char
is tested with conditionals.char
lookup table. This will help for programs that must transform lots of text.using System; using System.Diagnostics; static class Rot13 { static char[] _shift = new char[char.MaxValue]; public static void Init() { // Default is not transformed. for (int i = 0; i < char.MaxValue; i++) { _shift[i] = TransformAt((char)i); } } public static string TransformWithTable(string value) { // Convert to char array. char[] array = value.ToCharArray(); // Shift each character. for (int i = 0; i < array.Length; i++) { array[i] = _shift[array[i]]; } // Return new string. return new string(array); } public static string Transform(string value) { char[] array = value.ToCharArray(); for (int i = 0; i < array.Length; i++) { array[i] = TransformAt(array[i]); } return new string(array); } static char TransformAt(char value) { int number = (int)value; if (number >= 'a' && number <= 'z') { if (number > 'm') { number -= 13; } else { number += 13; } } else if (number >= 'A' && number <= 'Z') { if (number > 'M') { number -= 13; } else { number += 13; } } return (char)number; } } class Program { const int _max = 1000000; static void Main() { Rot13.Init(); Console.WriteLine(Rot13.Transform("bird is RED")); Console.WriteLine(Rot13.TransformWithTable("bird is RED")); var s1 = Stopwatch.StartNew(); // Version 1: use if-statements to apply ROT13. for (int i = 0; i < _max; i++) { if (Rot13.Transform("bird is RED") == "") { return; } } s1.Stop(); // Version 2: use lookup table for ROT13. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { if (Rot13.TransformWithTable("bird is RED") == "") { return; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }oveq vf ERQ oveq vf ERQ 70.60 ns Rot13.Transform 47.35 ns Rot13.TransformWithTable
Let's look at the input and output required. ROT13 stands for rotate 13, and it rotates each character in text by 13 places. The method is a primitive cipher.
The apartment is 700 square feet. Gur ncnegzrag vf 700 fdhner srrg.
We applied the ROT13 transformation to a string
. The first example here highlights the benefits of ToCharArray
, which can also help with other kinds of character manipulation.