Sha256: e071302434db451f7568016b17b122f5ece0706d28aa52d44c7e00a498f82480

Contents?: true

Size: 1001 Bytes

Versions: 115

Compression:

Stored size: 1001 Bytes

Contents

using System.Collections.Generic;
using System.Linq;

public class Atbash
{
    private const string PLAIN = "abcdefghijklmnopqrstuvwxyz";
    private const string CIPHER = "zyxwvutsrqponmlkjihgfedcba";

    public static string Encode(string value)
    {
        var encoded = string.Concat(StripInvalidCharacters(value).ToLower().Select(ApplyCipher));
        return SplitIntoFiveLetterWords(encoded);
    }

    private static string StripInvalidCharacters(string value)
    {
        return string.Concat(value.Where(char.IsLetterOrDigit));
    }

    private static char ApplyCipher(char value)
    {
        var idx = PLAIN.IndexOf(value);
        return idx >= 0 ? CIPHER[idx] : value;
    }

    private static string SplitIntoFiveLetterWords(string value)
    {
        var words = new List<string>();

        for (int i = 0; i < value.Length; i += 5)
            words.Add(i + 5 <= value.Length ? value.Substring(i, 5) : value.Substring(i));

        return string.Join(" ", words);
    }
}

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
trackler-2.0.8.14 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.13 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.12 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.11 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.10 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.9 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.8 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.7 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.6 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.5 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.4 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.3 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.2 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.8.1 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.7.0 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.44 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.43 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.42 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.41 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.40 tracks/csharp/exercises/atbash-cipher/Example.cs