Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shift Cipher Range out of Bounds
- #define LETTER_POS 97
- #define ALPHABET_LENGTH 26
- - (NSString*)encode:(NSString*)original withShift:(int)shift {
- NSMutableString* encoded = [NSMutableString stringWithString:original];
- for (int i=0; i < [encoded length]; i++) {
- char oriChar = [encoded characterAtIndex:i];
- if (oriChar == ' ') {
- continue;
- }
- char encChar = ((oriChar - LETTER_POS) + shift) % ALPHABET_LENGTH + LETTER_POS;
- NSRange range = {i, i+1};
- [encoded replaceCharactersInRange:range withString:[NSString stringWithFormat:@"%c" , encChar]];
- }
- return encoded;
- }
Add Comment
Please, Sign In to add comment