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.
omg
gives trl
c
gives c
Cool
gives Cool
The quick brown fox jumps over the lazy dog.
gives Gur dhvpx oebja sbk whzcf bire gur ynml qbt.
Gur dhvpx oebja sbk whzcf bire gur ynml qbt.
gives The quick brown fox jumps over the lazy dog.
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
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.
// 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.Linq;
using System.Collections.Generic;
using System.Text;
public static class RotationalCipher
{
private static char[] _alphas_lower = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
public static string Rotate(string text, int shiftKey)
{
var cipher_lower = new List<char>();
cipher_lower.AddRange(_alphas_lower.ToList().GetRange(shiftKey, _alphas_lower.Count() - shiftKey));
cipher_lower.AddRange(_alphas_lower.ToList().GetRange(0, shiftKey));
var cipher = _alphas_lower
.Zip(cipher_lower, (first, second) => (first, second))
.ToDictionary(d => d.first, d => d.second);
StringBuilder text_cipher = new StringBuilder();
foreach (var c in text)
{
if (cipher.TryGetValue(Char.ToLowerInvariant(c), out var value))
{
value = Char.IsLower(c) ? value : Char.ToUpperInvariant(value);
text_cipher.Append(value);
}
else
{
text_cipher.Append(c);
}
}
return text_cipher.ToString();
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments