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.180 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.179 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.178 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.177 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.176 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.175 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.174 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.173 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.172 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.171 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.170 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.169 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.167 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.166 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.165 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.164 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.163 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.162 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.161 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.160 tracks/cpp/exercises/atbash-cipher/example.cpp