 # freddie2025's solution

## to Rotational Cipher in the C# Track

Published at Apr 15 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. Using a key of `0` or `26` will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`. The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
``````

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

• ROT5 `omg` gives `trl`
• ROT0 `c` gives `c`
• ROT26 `Cool` gives `Cool`
• ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
• ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit RotationalCipher.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

### RotationalCipherTest.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class RotationalCipherTest
{
[Fact]
public void Rotate_a_by_0_same_output_as_input()
{
Assert.Equal("a", RotationalCipher.Rotate("a", 0));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_a_by_1()
{
Assert.Equal("b", RotationalCipher.Rotate("a", 1));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_a_by_26_same_output_as_input()
{
Assert.Equal("a", RotationalCipher.Rotate("a", 26));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_m_by_13()
{
Assert.Equal("z", RotationalCipher.Rotate("m", 13));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_n_by_13_with_wrap_around_alphabet()
{
Assert.Equal("a", RotationalCipher.Rotate("n", 13));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_capital_letters()
{
Assert.Equal("TRL", RotationalCipher.Rotate("OMG", 5));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_spaces()
{
Assert.Equal("T R L", RotationalCipher.Rotate("O M G", 5));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_numbers()
{
Assert.Equal("Xiwxmrk 1 2 3 xiwxmrk", RotationalCipher.Rotate("Testing 1 2 3 testing", 4));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_punctuation()
{
Assert.Equal("Gzo'n zvo, Bmviyhv!", RotationalCipher.Rotate("Let's eat, Grandma!", 21));
}

[Fact(Skip = "Remove to run test")]
public void Rotate_all_letters()
{
Assert.Equal("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.", RotationalCipher.Rotate("The quick brown fox jumps over the lazy dog.", 13));
}
}``````
``````using System;
using System.Text;

public static class RotationalCipher
{
public static string Rotate(string text, int shiftKey)
{
StringBuilder sb = new StringBuilder();

foreach(char c in text)
{
int rollbackPoint = Char.IsLower(c) ? 122 : 90;

if(Char.IsLetter(c))
sb.Append(c + shiftKey > rollbackPoint ? (char)(c + shiftKey - 26) : (char)(c + shiftKey));
else
sb.Append(c);
}

return sb.ToString();
}
}``````