Sha256: 351390a4777cb4a2c43a8aa02b95f3dca66866a97bd12589aedbd48fb30e135b

Contents?: true

Size: 1.75 KB

Versions: 268

Compression:

Stored size: 1.75 KB

Contents

#import "AtbashCipherExample.h"

static NSDictionary<NSString *, NSString *> *cipherDictionary;

@implementation AtbashCipher

+ (void)initialize {
    if (self == [AtbashCipher class]) {
        cipherDictionary = [self createCipherDictionary];
    }
}

+ (NSDictionary<NSString *, NSString *> *)createCipherDictionary {
    NSArray<NSString *> *alphabet = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z"];
    NSMutableDictionary<NSString *, NSString *> *cipherDict = [[NSMutableDictionary alloc] initWithCapacity:26];
    
    NSUInteger count = alphabet.count;
    
    for (int i = 0; i < count; i++) {
        NSString *key = alphabet[i];
        NSString *value = alphabet[count - i - 1];
        
        cipherDict[key] = value;
    }

    return cipherDict;
}

+ (NSString *)encode:(NSString *)input {
    input = [input lowercaseString]; //!OCLint
    
    NSCharacterSet *characterSet = [NSCharacterSet alphanumericCharacterSet];
    NSMutableString *result = [[NSMutableString alloc] init];
    int count = 0;
    
    for (int i = 0; i < input.length; i++) {
        unichar character = [input characterAtIndex:i];
        if (![characterSet characterIsMember:character]) {
            continue;
        }
        
        if (count % 5 == 0 && count > 0) {
            [result appendString:@" "];
        }
        
        NSString *string = [NSString stringWithFormat:@"%C", character];
        NSString *resultLetter = cipherDictionary[string];
        
        if (resultLetter) {
            [result appendString:resultLetter];
        } else {
            [result appendString:string];
        }
        
        count++;
    }
    
    return result;
}

@end

Version data entries

268 entries across 268 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.138 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.137 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.136 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.135 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.134 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.133 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.132 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.131 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.130 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.129 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.128 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.127 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.126 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.125 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.124 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.123 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.122 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.121 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m
trackler-2.2.1.120 tracks/objective-c/exercises/atbash-cipher/AtbashCipherExample.m