🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of stevenhuey

stevenhuey's solution

to Atbash Cipher in the Objective-C Track

Published at Jul 13 2018 · 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


There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/atbash-cipher/AtbashCipher.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


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.


#import <XCTest/XCTest.h>

#if __has_include("AtbashCipherExample.h")
# import "AtbashCipherExample.h"
# else
# import "AtbashCipher.h"

@interface AtbashCipherTest : XCTestCase


@implementation AtbashCipherTest

- (void)testEncodeNo {
    XCTAssertEqualObjects(@"ml", [AtbashCipher encode:@"no"]);

- (void)testEncodeYes {
    XCTAssertEqualObjects(@"bvh", [AtbashCipher encode:@"yes"]);

- (void)testEncodeOMG {
    XCTAssertEqualObjects(@"lnt", [AtbashCipher encode:@"OMG"]);

- (void)testEncodeOMGWithSpaces {
    XCTAssertEqualObjects(@"lnt", [AtbashCipher encode:@"O M G"]);

- (void)testEncodeLongWord {
    XCTAssertEqualObjects(@"nrmwy oldrm tob", [AtbashCipher encode:@"mindblowingly"]);

- (void)testEncodeNumbers {
    XCTAssertEqualObjects(@"gvhgr mt123 gvhgr mt", [AtbashCipher encode:@"Testing, 1 2 3, testing."]);

- (void)testEncodeSentence {
    XCTAssertEqualObjects(@"gifgs rhurx grlm", [AtbashCipher encode:@"Truth is fiction."]);

- (void)testEncodeAllTheThings {
    NSString *plaintext = @"The quick brown fox jumps over the lazy dog.";
    NSString *cipher = @"gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt";
    XCTAssertEqualObjects(cipher, [AtbashCipher encode:plaintext]);

//  AtbashCipher.m
//  AtbashCipher
//  Created by Steve Huey on 6/15/17.
//  Copyright © 2017 Steve Huey. All rights reserved.

#import "AtbashCipher.h"

@interface AtbashCipher ()
+ (NSInteger)indexOfCharacter:(unichar)c inString:(NSString*)string;
+ (NSString*)groupEncoding:(NSString*)encodedString;

@implementation AtbashCipher
+ (NSString*)encode:(NSString*)phrase
   NSMutableString* encodedPhrase = [NSMutableString stringWithCapacity:0];
   NSString* alphabet = @"abcdefghijklmnopqrstuvwxyz";
   NSString* lowercasePhrase = [phrase lowercaseString];
   NSCharacterSet* letters = [NSCharacterSet letterCharacterSet];
   NSCharacterSet* numbers = [NSCharacterSet decimalDigitCharacterSet];
   for (NSUInteger index = 0; index < [lowercasePhrase length]; ++index)
      unichar c = [lowercasePhrase characterAtIndex:index];
      if ([letters characterIsMember:c])
         NSInteger locationInAlphabet = [[self class] indexOfCharacter:c
         NSInteger indexFromEndOfString = [alphabet length] - locationInAlphabet - 1;
         [encodedPhrase appendFormat:@"%c", [alphabet characterAtIndex:indexFromEndOfString]];
      else if ([numbers characterIsMember:c])
         [encodedPhrase appendFormat:@"%c", c];
   return [[self class] groupEncoding:encodedPhrase];

+ (NSInteger)indexOfCharacter:(unichar)c inString:(NSString*)string
   NSRange range = [string rangeOfString:[NSString stringWithFormat:@"%c", c]];
   return range.location;

+ (NSString*)groupEncoding:(NSString*)encodedString
   NSMutableString* groupedString = [NSMutableString stringWithCapacity:[encodedString length]];
   for (NSUInteger index = 0; index < [encodedString length]; ++index)
      [groupedString appendFormat:@"%c", [encodedString characterAtIndex:index]];
      if ((index + 1) % 5 == 0)
         [groupedString appendString:@" "];
   return [groupedString stringByTrimmingCharactersInSet:
           [NSCharacterSet whitespaceCharacterSet]];

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?