🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # cristos667's solution

## to Atbash Cipher in the C Track

Published at May 19 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 "Guides" section of the C track on the Exercism site. 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.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need 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, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

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

### test_atbash_cipher.c

``````#include "vendor/unity.h"
#include "../src/atbash_cipher.h"
#include <stdlib.h>
#include <string.h>

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_encode(char *input, char *expected)
{
char *result = atbash_encode(input);

TEST_ASSERT_EQUAL_STRING(expected, result);

free(result);
}

static void test_decode(char *input, char *expected)
{
char *result = atbash_decode(input);

TEST_ASSERT_EQUAL_STRING(expected, result);

free(result);
}

void test_encode_yes(void)
{
test_encode("yes", "bvh");
}

void test_encode_no(void)
{
TEST_IGNORE();
test_encode("no", "ml");
}

void test_encode_OMG(void)
{
TEST_IGNORE();
test_encode("OMG", "lnt");
}

void test_encode_spaces(void)
{
TEST_IGNORE();
test_encode("O M G", "lnt");
}

void test_encode_mindblowingly(void)
{
TEST_IGNORE();
test_encode("mindblowingly", "nrmwy oldrm tob");
}

void test_encode_numbers(void)
{
TEST_IGNORE();
test_encode("Testing,1 2 3, testing.", "gvhgr mt123 gvhgr mt");
}

void test_encode_deep_thought(void)
{
TEST_IGNORE();
test_encode("Truth is fiction.", "gifgs rhurx grlm");
}

void test_encode_all_the_letters(void)
{
TEST_IGNORE();
test_encode("The quick brown fox jumps over the lazy dog.",
"gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
}

void test_encode_ignores_non_ascii(void)
{
TEST_IGNORE();
test_encode("non ascii éignored", "mlmzh xrrrt mlivw");
}

void test_decode_exercism(void)
{
TEST_IGNORE();
test_decode("vcvix rhn", "exercism");
}

void test_decode_a_sentence(void)
{
TEST_IGNORE();
test_decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v",
"anobstacleisoftenasteppingstone");
}

void test_decode_numbers(void)
{
TEST_IGNORE();
test_decode("gvhgr mt123 gvhgr mt", "testing123testing");
}

void test_decode_all_the_letters(void)
{
TEST_IGNORE();
test_decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt",
"thequickbrownfoxjumpsoverthelazydog");
}

int main(void)
{
UnityBegin("test/test_atbash_cipher.c");

RUN_TEST(test_encode_yes);
RUN_TEST(test_encode_no);
RUN_TEST(test_encode_OMG);
RUN_TEST(test_encode_spaces);
RUN_TEST(test_encode_mindblowingly);
RUN_TEST(test_encode_numbers);
RUN_TEST(test_encode_deep_thought);
RUN_TEST(test_encode_all_the_letters);
RUN_TEST(test_encode_ignores_non_ascii);

RUN_TEST(test_decode_exercism);
RUN_TEST(test_decode_a_sentence);
RUN_TEST(test_decode_numbers);
RUN_TEST(test_decode_all_the_letters);

return UnityEnd();
}``````

### src/atbash_cipher.h

``````#ifndef ATBASH_CIPHER_INCL
#define ATBASH_CIPHER_INCL

#define GROUP_SIZE (5)

char *atbash_encode(const char *input);
char *atbash_decode(const char *input);

#endif``````

### src/atbash_cipher.c

``````#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>

#include "atbash_cipher.h"

static char *atbash(const char *input, bool group)
{
if (input == NULL)
return NULL;

int input_len = 0;
int output_len = 0;
int letters = 0;
for (int i = 0; input[i] != '\0'; i++)
{
input_len++;
if (isalnum(input[i]))
output_len++;
}

output_len += (output_len / GROUP_SIZE) + 1;

char *ciphertext = malloc(output_len * sizeof(char));
if (ciphertext == NULL)
return NULL;

int j = 0;
for (int i = 0; i < input_len; i++)
{
if (isalnum(input[i]))
{
if (group && letters > 0 && letters % GROUP_SIZE == 0)
{
ciphertext[j++] = ' ';
letters = 0;
}
if (isdigit(input[i]))
{
//copy without cipher
ciphertext[j++] = input[i];
letters++;
}
else
{
ciphertext[j++] = 'z' - (tolower(input[i]) - 'a');
letters++;
}
}
}
ciphertext[j] = '\0';
return ciphertext;
}

char *atbash_encode(const char *input)
{
return atbash(input, true);
}

char *atbash_decode(const char *input)
{
return atbash(input, false);
}``````