Avatar of BallKa

BallKa's solution

to Atbash Cipher in the C++ Track

Published at Aug 11 2019 · 0 comments
Test suite


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.


  • 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.


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.


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.


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

    REQUIRE("bvh" == atbash::encode("yes"));

    REQUIRE("ml" == atbash::encode("no"));

    REQUIRE("lnt" == atbash::encode("OMG"));

    REQUIRE("lnt" == atbash::encode("O M G"));

    REQUIRE("nrmwy oldrm tob" == atbash::encode("mindblowingly"));

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

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

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

    REQUIRE("exercism" == atbash::decode("vcvix rhn"));

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

    REQUIRE("testing123testing" == atbash::decode("gvhgr mt123 gvhgr mt"));

    REQUIRE("thequickbrownfoxjumpsoverthelazydog" ==
                        atbash::decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"));




#include <string>

namespace atbash {

    std::string encode(const std::string& text);

    std::string decode(const std::string& cipher);




#include "atbash_cipher.h"

using namespace std;

static char atbash_cipher(char c){
    return 'a' + 'z' - c;

string atbash::encode(const string& text){
    string cipher;
    for(char c: text){
        c = tolower(c);
        if('a' <= c && c <= 'z')
        else if('0' <= c && c <= '9')
        if((cipher.size()+1)%6 == 0)
            cipher.push_back(' ');
    if(cipher.back() == ' ')
    return cipher;

string atbash::decode(const string& cipher){
    string text;
    for(char c: cipher)
        if('a' <= c && c <= 'z')
        else if('0' <= c && c <= '9')
    return text;


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?