Problem. You want to develop an ROT13 transformation cipher, which shifts letters 13 places in the alphabet. Generally, change the individual characters in a string based on logic. Solution. Here we implement this transform in the C# programming language, first looking at the requirements of the algorithm and then an example program.
First, here we review the ROT13 cipher and 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 similar to the one used by Julius Caesar, dictator of the Roman Republic, as the Caesar Cipher.
Input: There was a cute dog in 2008. [1]
Gurer jnf n phgr qbt va 2008. [2]
ROT13 tranformed: Gurer jnf n phgr qbt va 2008. [1]
There was a cute dog in 2008. [2]Here we look at a simple implementation of ROT13 in C#. The version we see here simply tests each character, and adds to a character buffer. Note that you could use StringBuilder instead, but it is much slower for single character appends.
=== Example program that implements ROT13 (C#) ===
using System;
class Program
{
static void Main()
{
string a = "There was a cute dog in 2008.";
Console.WriteLine(a);
a = Rot13.Transform(a);
Console.WriteLine(a);
a = Rot13.Transform(a);
Console.WriteLine(a);
Console.Read();
}
static class Rot13
{
/// <summary>
/// Performs the ROT13 character rotation.
/// </summary>
public static string Transform(string value)
{
char[] c = value.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
int t = (int)c[i];
if (t >= 'a' && t <= 'z')
{
if (t >= 'm')
{
t -= 13;
}
else
{
t += 13;
}
}
else if (t >= 'A' && t <= 'Z')
{
if (t >= 'M')
{
t -= 13;
}
else
{
t += 13;
}
}
c[i] = (char)t;
}
return new string(c);
}
}
}
=== Output of the program ===
There was a cute dog in 2008.
Gurer jnf n phgr qbt va 2008.
There was a cute dog in 2008.Overview of the method. It uses a static class. Static classes in C# are an elegant way to encapsulate static methods and behaviors. The ROT13 Transform method here doesn't store state, so it is best placed logically in a static class.
Using ToCharArray method. It calls ToCharArray. The reference hyperlink provides some detail on the performance of ToCharArray. [C# ToCharArray Method - dotnetperls.com]
Final steps. It casts the char to an integer and then transforms that value. Finally, it casts back to a char. It uses the new string constructor. We see how a character array can be converted into a string. [C# Convert Char Array to String - dotnetperls.com]
The ROT13 algorithm that utilizes a char lookup table is in a separate article. It performs better but has a much higher initialization cost. Therefore it is ideal only for programs that transform huge amounts of text. [C# Char Lookup Table - dotnetperls.com]
Here we looked at how you can apply the ROT13 transformation to a string using the C# programming language. Example 1 here highlights the benefits of ToCharArray, which can also help a lot with other kinds of character manipulation. It provides the mutable buffer from the immutable string parameter.