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

jaishreedhage'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.h
//  AtbashCipher
//  Created by Jaishree Dhage on 10/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.

#import <Foundation/Foundation.h>

@interface AtbashCipher : NSObject

+(NSString *) encode:(NSString *)string;



//  AtbashCipher.m
//  AtbashCipher
//  Created by Jaishree Dhage on 10/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.

#import "AtbashCipher.h"

@implementation AtbashCipher

+(NSString *)encode:(NSString *)string{
    //NSString *plain = @"abcdefghijklmnopqrstuvwxyz";
    NSString *cipher = @"zyxwvutsrqponmlkjihgfedcba";
    string = [[string componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];
    string = [string lowercaseString];
    for(int i=0,k=0;i<string.length;i++){
        if([string characterAtIndex:i]>='a' && [string characterAtIndex:i]<='z')
            string = [string stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",[cipher characterAtIndex:(int)[string characterAtIndex:i]-97]]];
        if(k%5==0 && i+1<string.length){
            string = [NSString stringWithFormat:@"%@ %@",[string substringToIndex:i+1],[string substringFromIndex:i+1]];
    return string;


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?