
You want to develop a char lookup table to process specific characters in your C# string. Lookup tables are simple character-based associative arrays. They can eliminate costly branches in your character manipulation code.
Lookup table for ROT13 benchmark ROT13 lookup table: 1779 ms [faster] ROT13 transformation: 3447 ms

As an aside, the approach to building a lookup table here can be adapted to many ciphers, including the Atbash cipher and Caesar ciphers. Here, we see the ROT13 lookup table. Using a lookup table, an array that contains transformed values for each index value, is faster in many cases for integer and char transformations and shifts. It has some setup time required, however.
This C# example shows how to create a char lookup table to optimize character conversions.
Program that transforms using ROT13 [C#]
/// <summary>
/// Contains the ROT13 text cipher code.
/// </summary>
class Rot13Table
{
/// <summary>
/// Lookup table to shift characters.
/// </summary>
char[] _shift = new char[char.MaxValue];
/// <summary>
/// Generates the lookup table.
/// </summary>
public Rot13Table()
{
// Set these as the same.
for (int i = 0; i < char.MaxValue; i++)
{
_shift[i] = (char)i;
}
// Specify capital letters are shifted.
for (char c = 'A'; c <= 'Z'; c++)
{
char x;
if (c <= 'M')
{
x = (char)(c + 13);
}
else
{
x = (char)(c - 13);
}
_shift[(int)c] = x;
}
// Specify lowercase letters are shifted.
for (char c = 'a'; c <= 'z'; c++)
{
char x;
if (c <= 'm')
{
x = (char)(c + 13);
}
else
{
x = (char)(c - 13);
}
_shift[(int)c] = x;
}
}
/// <summary>
/// Rotate the specified text with ROT13.
/// </summary>
public string Transform(string value)
{
try
{
// Convert to char array.
char[] a = value.ToCharArray();
// Shift each letter we need to shift.
for (int i = 0; i < a.Length; i++)
{
int t = (int)a[i];
a[i] = _shift[t];
}
// Return new string.
return new string(a);
}
catch
{
// Just return original value on failure.
return value;
}
}
}Description. The constructor generates the lookup table. You can see the lookup table is initialized in the same way that a ROT13 loop rotates its input. It has a Transform method. This is not a static method because it must store the state of the rotation lookup table. Here next we see the calling method.
Program that uses ROT13 translation [C#]
using System;
class Program
{
static void Main()
{
string a = "There was a cute dog in 2008.";
Console.WriteLine(a);
Rot13Table t = new Rot13Table();
a = t.Transform(a);
Console.WriteLine(a);
a = t.Transform(a);
Console.WriteLine(a);
}
}
Output
There was a cute dog in 2008.
Gurer jnf n phgr qbt va 2008.
There was a cute dog in 2008.
My hypothesis from working with C for years was that the lookup table would be faster versus the basic ROT13 loop. I benchmarked the above two versions with the same string used in the examples. Please see the figures at the top of this article.

Tip. You can use lookup tables for fast substitution of any valid character for another. Alternatively, you can use char lookup tables to access an int or other type based on the char being processed.
Here we saw how you can use a class instance in the C# language to encapsulate a lookup table. This provides superior performance to if statements in a loop. Look at the conventional ROT13 table on this site.
ROT13 Char Type