TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# ROT13 Method

This C# example method implements the ROT13 transformation cipher.

ROT13 is a cipher.

This transformation cipher shifts letters 13 places in the alphabet. Generally, we can change the individual characters in a string based on logic. Many implementations are possible.

Intro. 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.

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

ROT13 transformation: before/after

There was a cute dog in 2008.
Gurer jnf n phgr qbt va 2008.

Example. Here we look at a simple implementation of ROT13. The version we see here simply 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

C# program that implements ROT13

using System;

class Program
{
    static void Main()
    {
	string value = "There was a cute dog in 2008. (Zamborine)";
	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

There was a cute dog in 2008. (Zamborine)
Gurer jnf n phgr qbt va 2008. (Mnzobevar)
There was a cute dog in 2008. (Zamborine)

The ROT13 Transform method here doesn't store state, so it is logically placed in a static class. It calls ToCharArray. The reference page provides some detail on the performance of ToCharArray.

Static ClassToCharArray

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.

Convert Char Array to String

Bug: The method implementation in this article contained a serious bug that would actually 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.

Warning: This shows us that you cannot trust any code in books or on the Internet except code you have personally tested and inspected.

Lookup table. The ROT13 algorithm that uses a char lookup table is in a separate article. It performs better but has a much higher initialization cost. It is ideal only for programs that transform huge amounts of text.

Char Lookup Table

Summary. Here we looked at how you can apply the ROT13 transformation to a string using the C# language. The first example here highlights the benefits of ToCharArray, which can also help with other kinds of character manipulation.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf