Avatar of Quandra

Quandra's solution

to Atbash Cipher in the C++ Track

Published at Oct 31 2019 · 0 comments
Test suite

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++14 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_cipher::encode("yes"));

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

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

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

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

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

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

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

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

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

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

    REQUIRE("thequickbrownfoxjumpsoverthelazydog" ==
                        atbash_cipher::decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"));
#if !defined(ATBASH_CIPHER_H)
using namespace std;

namespace atbash_cipher{
  string decode(string s,bool space=false){
    string ans="";
    int sz=(int)s.size(),i,osz=0;
      char c=tolower(s[i]);
        if(space && osz && osz%5==0) ans+=' ';
        if(isdigit(c)) ans+=c;
        else ans+=(219-c);
    return ans;
  string encode(string s){
    return decode(s,true);
}  // namespace atbash_cipher

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?