Created
June 15, 2021 18:35
-
-
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
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
// 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