Home
Map
ROT13 MethodImplement the ROT13 transformation cipher. Use ToCharArray and a char lookup table.
C#
This page was last reviewed on Jan 25, 2022.
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.
An example. 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.
for
StringBuilder
Tip Be sure to evaluate the char lookup table method, which is faster, although somewhat more complex.
Detail This method does not store state, so it is placed in a static class. It calls the ToCharArray method.
String ToCharArray
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
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.
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.
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
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
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 25, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.