Created
February 6, 2016 12:42
-
-
Save dautermann/d935f9a65bc64e5ca5e6 to your computer and use it in GitHub Desktop.
You and your friend have a code language. To encode a message you replace every letter with another letter 2 ahead of it. E.g. “ENCODED MESSAGE” becomes “GPEQFGF OGUUCIG”. Input: “ENCODED MESSAGE” Output: “GPEQFGF OGUUCIG”
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface EncryptDecrypt (NSString) | |
- (NSString *) encrypt; | |
- (NSString *) decrypt; | |
@end | |
@implementation EncryptDecrypt { | |
- (NSString *) bumpCharacters: (BOOL) bumpUp | |
{ | |
NSInteger length = self.length; | |
NSMutableString *mutableCopyOfMyself = [[NSMutableString alloc] init]; | |
for(NSInteger index = 0; index < length; index++) | |
{ | |
unichar theCharacter = [self characterAtIndex: index]; | |
// we need to verify that theCharacter is within the valid ASCII range ; 32-255 | |
// otherwise don't do anything | |
if bumpUp { | |
theCharacter = theCharacter+2 | |
if theCharacter > 'Z' | |
theCharacter-=26 | |
} else { | |
if theCharacter < 'C' | |
theCharacter+=26 | |
theCharacter = theCharacter-2 | |
} | |
[mutableCopyOfMyself appendFormat: @"%c", theCharacter]; | |
} | |
return mutableCopyOfMyself; | |
} | |
- (NSString *) encrypt { | |
[self bumpCharacters: TRUE]; | |
} | |
- (NSString *) decrypt { | |
[self bumpCharacters: FALSE]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment