Guest User

Untitled

a guest
Aug 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. Shift Cipher Range out of Bounds
  2. #define LETTER_POS 97
  3. #define ALPHABET_LENGTH 26
  4.  
  5. - (NSString*)encode:(NSString*)original withShift:(int)shift {
  6.  
  7. NSMutableString* encoded = [NSMutableString stringWithString:original];
  8. for (int i=0; i < [encoded length]; i++) {
  9. char oriChar = [encoded characterAtIndex:i];
  10. if (oriChar == ' ') {
  11. continue;
  12. }
  13. char encChar = ((oriChar - LETTER_POS) + shift) % ALPHABET_LENGTH + LETTER_POS;
  14. NSRange range = {i, i+1};
  15. [encoded replaceCharactersInRange:range withString:[NSString stringWithFormat:@"%c" , encChar]];
  16.  
  17. }
  18. return encoded;
  19.  
  20. }
Add Comment
Please, Sign In to add comment