-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
52 lines (47 loc) · 1.65 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Linq;
namespace CaesarCipher
{
/// <summary>
/// The Caesar cipher is a basic encryption technique used by Julius Caesar to securely communicate with his generals.
/// Each letter is replaced by another letter N positions down the english alphabet.
/// For example, for a rotation of 5, the letter 'c' would be replaced by an 'h'.
/// In case of a 'z', the alphabet rotates and it is transformed into a 'd'.
/// Implement a decoder for the Caesar cipher where N = 4.
/// </summary>
class Program
{
static void Main(string[] args)
{
var list = new[]
{
"dahhk sknhz",
"sexxha",
"iannu ydneopiwo"
};
foreach (var message in list)
{
var result = Decode(message, 4);
Console.WriteLine(result);
}
}
private static string Decode(string input, int n)
{
const char start = 'a';
const char end = 'z';
const int length = end - start + 1;
var charArray = input.ToLowerInvariant().Select(c =>
{
if (!char.IsLetter(c))
return c;
var shiftedChar = c + n;
if (shiftedChar < start)
shiftedChar = shiftedChar + length;
else if (shiftedChar > end)
shiftedChar = shiftedChar - length;
return (char)shiftedChar;
}).ToArray();
return new String(charArray);
}
}
}