| 1 | /********************************************************************* |
| 2 | * Filename: rot-13.c |
| 3 | * Author: Brad Conte (brad AT bradconte.com) |
| 4 | * Copyright: |
| 5 | * Disclaimer: This code is presented "as is" without any guarantees. |
| 6 | * Details: Implementation of the ROT-13 encryption algorithm. |
| 7 | Algorithm specification can be found here: |
| 8 | * |
| 9 | This implementation uses little endian byte order. |
| 10 | *********************************************************************/ |
| 11 | |
| 12 | /*************************** HEADER FILES ***************************/ |
| 13 | #include <string.h> |
| 14 | #include "rot-13.h" |
| 15 | |
| 16 | /*********************** FUNCTION DEFINITIONS ***********************/ |
| 17 | void rot13(char str[]) |
| 18 | { |
| 19 | int case_type, idx, len; |
| 20 | |
| 21 | for (idx = 0, len = strlen(str); idx < len; idx++) { |
| 22 | // Only process alphabetic characters. |
| 23 | if (str[idx] < 'A' || (str[idx] > 'Z' && str[idx] < 'a') || str[idx] > 'z') |
| 24 | continue; |
| 25 | // Determine if the char is upper or lower case. |
| 26 | if (str[idx] >= 'a') |
| 27 | case_type = 'a'; |
| 28 | else |
| 29 | case_type = 'A'; |
| 30 | // Rotate the char's value, ensuring it doesn't accidentally "fall off" the end. |
| 31 | str[idx] = (str[idx] + 13) % (case_type + 26); |
| 32 | if (str[idx] < 26) |
| 33 | str[idx] += case_type; |
| 34 | } |
| 35 | } |