ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# herman-nutest's solution

## to Atbash Cipher in the C++ Track

Published at Jun 10 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards. The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba
``````

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic substitution cipher. However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.

## Examples

• Encoding `test` gives `gvhg`
• Decoding `gvhg` gives `test`
• Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`

## Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

``````#if defined(EXERCISM_RUN_ALL_TESTS)
``````

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++11 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.

## Source

Wikipedia http://en.wikipedia.org/wiki/Atbash

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### atbash_cipher_test.cpp

``````#include "atbash_cipher.h"
#include "test/catch.hpp"

TEST_CASE("encode_yes")
{
REQUIRE("bvh" == atbash::encode("yes"));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("encode_no")
{
REQUIRE("ml" == atbash::encode("no"));
}

TEST_CASE("encode_OMG")
{
REQUIRE("lnt" == atbash::encode("OMG"));
}

TEST_CASE("encode_spaces")
{
REQUIRE("lnt" == atbash::encode("O M G"));
}

TEST_CASE("encode_mindblowingly")
{
REQUIRE("nrmwy oldrm tob" == atbash::encode("mindblowingly"));
}

TEST_CASE("encode_numbers")
{
REQUIRE("gvhgr mt123 gvhgr mt" == atbash::encode("Testing,1 2 3, testing."));
}

TEST_CASE("encode_deep_thought")
{
REQUIRE("gifgs rhurx grlm" == atbash::encode("Truth is fiction."));
}

TEST_CASE("encode_all_the_letters")
{
REQUIRE("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" ==
atbash::encode("The quick brown fox jumps over the lazy dog."));
}

TEST_CASE("decode_exercism")
{
REQUIRE("exercism" == atbash::decode("vcvix rhn"));
}

TEST_CASE("decode_a_sentence")
{
REQUIRE("anobstacleisoftenasteppingstone" ==
atbash::decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v"));
}

TEST_CASE("decode_numbers")
{
REQUIRE("testing123testing" == atbash::decode("gvhgr mt123 gvhgr mt"));
}

TEST_CASE("decode_all_the_letters")
{
REQUIRE("thequickbrownfoxjumpsoverthelazydog" ==
atbash::decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"));
}
#endif``````

### atbash_cipher.h

``````#pragma once
#include <string>

namespace atbash {
std::string encode(std::string in);
std::string decode(std::string in);
}``````

### atbash_cipher.cpp

``````#include "atbash_cipher.h"
#include <algorithm>

namespace atbash {
std::string DECODER = "abcdefghijklmnopqrstuvwxyz0123456789";
std::string ENCODER = "zyxwvutsrqponmlkjihgfedcba0123456789";

std::string trim(std::string str)
{
size_t first = str.find_first_not_of(' ');
if (std::string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}

std::string encode(std::string in) {
std::string out;
int added_characters = 0; // Keep track of charater append to string
std::transform(in.begin(), in.end(), in.begin(), ::tolower);
for(char c: in) {
if( DECODER.find(c) != std::string::npos ) {
out += ENCODER[DECODER.find(c)];

//Add space every 5 characters
if( added_characters % 5 == 0 ) {
out += std::string(" ");
}
}
}
return trim(out);
}

std::string decode(std::string in) {
std::string out;
std::transform(in.begin(), in.end(), in.begin(), ::tolower);
for(char c: in) {
if( ENCODER.find(c) != std::string::npos ) {
out += DECODER[ENCODER.find(c)];
}
}
return out;
}
}``````