Sha256: 90fa7c5d046e8819376b916d0b78b9bfb8f55e238321fe63cbd8ee1b3de7def4

Contents?: true

Size: 1.06 KB

Versions: 185

Compression:

Stored size: 1.06 KB

Contents

#include <sstream>
#include <string>

#include "atbash_cipher.h"

namespace atbash {

const char ALPHA_START = 'a';
const char ALPHA_END = 'z';

char atbash_transform(char c)
{
    return std::isalpha(c) ? ALPHA_END - std::tolower(c) + ALPHA_START : c;
}

std::string encode(std::string const& plaintext)
{            
    int chunkSize = 5;
    int chunkCounter = 0;
    std::ostringstream os;

    for(int i = 0; i < plaintext.length(); i++) {
        char currentChar = plaintext.at(i);

        if (std::isalnum(currentChar)) {
            if ((chunkCounter > 0) && ((chunkCounter) % chunkSize == 0)) {
                os << " ";
            }
            
            os << atbash_transform(currentChar);
            chunkCounter++;
        }
    }

    return os.str();
}

std::string decode(std::string const& ciphertext)
{
    std::ostringstream os;

    for(int i = 0; i < ciphertext.length(); i++) {
        char currentChar = ciphertext.at(i);

        if (std::isalnum(currentChar)) {
            os << atbash_transform(currentChar);
        }
    }

    return os.str();
}

}

Version data entries

185 entries across 185 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.118 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.117 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.116 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.115 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.114 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.113 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.111 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.110 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.109 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.108 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.107 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.106 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.105 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.104 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.103 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.102 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.101 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.100 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.99 tracks/cpp/exercises/atbash-cipher/example.cpp