Skip to content

Instantly share code, notes, and snippets.

@dautermann
Created February 6, 2016 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dautermann/d935f9a65bc64e5ca5e6 to your computer and use it in GitHub Desktop.
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”
@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