HomeSearch

C# ROT13 Method, Char Lookup Table

This C# example page implements the ROT13 transformation cipher. It uses ToCharArray and a char lookup table.
ROT13. 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.Strings
An example. The version we see here tests each character, and adds to a character buffer. Note that you could instead use StringBuilder, but it is much slower for single character appends.StringBuilder

Tip: Be sure to evaluate the char lookup table method, which is faster, although somewhat more complex.

Transform: This method does not store state, so it is placed in a static class. It calls the ToCharArray method.

StaticToCharArray

Next: Transform() casts the char to an integer and then transforms that value. It casts back to a char, and uses the string constructor.

Convert Char Array, String
C# program that implements ROT13 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); Console.Read(); } 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); } } } Output The apartment is 700 square feet. Gur ncnegzrag vf 700 fdhner srrg. The apartment is 700 square feet.
Lookup table. 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.

Note: The lookup table here only works for ASCII text. But this is typical for ROT13 algorithms.

Version 1: This code uses the if-branch method where each char is tested with conditionals.

Version 2: This method uses the cached lookup table method, where we perform an array look up of the ROT13-transformed character.

Result: It is faster to use a char lookup table. This will help for programs that must transform lots of text.

C# program that uses char lookup table 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")); } } Output oveq vf ERQ oveq vf ERQ 70.60 ns Rot13.Transform 47.35 ns Rot13.TransformWithTable
An overview. 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.

Tip: It is similar to the one used by Julius Caesar, dictator of the Roman Republic, as the Caesar Cipher.

Caesar Cipher

Bug: The implementation in this article contained a bug that would corrupt data at the end of the transformation.

However: The bug was reported by a helpful reader and the method is now more likely to be correct.

ROT13 transformation before/after: The apartment is 700 square feet. Gur ncnegzrag vf 700 fdhner srrg.
A summary. 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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls