C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
StaticToCharArrayNext: 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, StringC# 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.
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
Tip: It is similar to the one used by Julius Caesar, dictator of the Roman Republic, as the Caesar Cipher.
Caesar CipherBug: 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.