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.98 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.97 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.96 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.95 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.94 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.93 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.92 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.91 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.90 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.89 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.88 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.87 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.86 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.85 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.84 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.83 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.82 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.81 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.80 tracks/cpp/exercises/atbash-cipher/example.cpp
trackler-2.2.1.79 tracks/cpp/exercises/atbash-cipher/example.cpp