Skip to content

Instantly share code, notes, and snippets.

@MCurran16
Created June 15, 2021 18:35
Show Gist options
  • Save MCurran16/6f1626db0b0f971f90de7404a5a355ef to your computer and use it in GitHub Desktop.
Save MCurran16/6f1626db0b0f971f90de7404a5a355ef to your computer and use it in GitHub Desktop.
Ceasar Cipher only accounts for letters. In this shift cipher, I shift all characters by a certain <shift> int
// Shift all characters by a certain <shift> int
-(NSString*) asciiShift:(int)shift {
if ([self length] == 0) {
return nil;
}
// no shift, return unencrypted string
if (shift == 0) {
return self;
}
NSMutableString* result = [NSMutableString stringWithString:self];
for (int i = 0; i < [result length]; i++) {
char curChar = [result characterAtIndex:i]; // NSString to ASCII int
if (curChar == ' ') {
continue;
}
char replacementChar = curChar + shift;
[result replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c", replacementChar]];
}
// clean result if needed
return [result stringByReplacingOccurrencesOfString:@" " withString:@""];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment