From 0e956c6e583ecf600603caacfee36f7763f6eeb1 Mon Sep 17 00:00:00 2001 From: Jeremias Moreira Gomes Date: Tue, 15 Nov 2022 21:22:16 -0300 Subject: [PATCH] chore: add Rot13 Cipher (#1008) * Add ROT13 cipher. * updating DIRECTORY.md * Fix suggestions. * Suggestions. Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal --- misc/rot13.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 misc/rot13.c diff --git a/misc/rot13.c b/misc/rot13.c new file mode 100644 index 00000000..1a6022f4 --- /dev/null +++ b/misc/rot13.c @@ -0,0 +1,60 @@ +/** + * @file + * @brief [ROT13](https://en.wikipedia.org/wiki/ROT13) is a simple letter + * substitution cipher that replaces a letter with the 13th letter after it in + * the alphabet. + * @details ROT13 transforms a piece of text by examining its alphabetic + * characters and replacing each one with the letter 13 places further along in + * the alphabet, wrapping back to the beginning if necessary. A becomes N, B + * becomes O, and so on up to M, which becomes Z, then the sequence continues at + * the beginning of the alphabet: N becomes A, O becomes B, and so on to Z, + * which becomes M. + * @author [Jeremias Moreira Gomes](https://github.com/j3r3mias) + */ + +#include /// for IO operations +#include /// for string operations +#include /// for assert + +/** + * @brief Apply the ROT13 cipher + * @param s contains the string to be processed + */ +void rot13(char *s) { + for (int i = 0; s[i]; i++) { + if (s[i] >= 'A' && s[i] <= 'Z') { + s[i] = 'A' + ((s[i] - 'A' + 13) % 26); + } else if (s[i] >= 'a' && s[i] <= 'z') { + s[i] = 'a' + ((s[i] - 'a' + 13) % 26); + } + } +} + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + char test_01[] = "The more I C, the less I see."; + rot13(test_01); + assert(strcmp(test_01, "Gur zber V P, gur yrff V frr.") == 0); + + char test_02[] = "Which witch switched the Swiss wristwatches?"; + rot13(test_02); + assert(strcmp(test_02, "Juvpu jvgpu fjvgpurq gur Fjvff jevfgjngpurf?") == 0); + + char test_03[] = "Juvpu jvgpu fjvgpurq gur Fjvff jevfgjngpurf?"; + rot13(test_03); + assert(strcmp(test_03, "Which witch switched the Swiss wristwatches?") == 0); + + printf("All tests have successfully passed!\n"); +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +}