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.6.39 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.38 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.37 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.36 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.35 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.34 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.33 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.32 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.31 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.30 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.29 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.28 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.27 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.26 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.25 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.24 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.23 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.22 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.21 tracks/csharp/exercises/atbash-cipher/Example.cs
trackler-2.0.6.20 tracks/csharp/exercises/atbash-cipher/Example.cs