From cd71ae9453171c411736be3ab6211d91794e0039 Mon Sep 17 00:00:00 2001 From: agbi623 Date: Wed, 2 Sep 2026 19:52:48 -0300 Subject: [PATCH 1/2] feat: add caesar cipher algorithm --- cipher/caesar_cipher.c | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 cipher/caesar_cipher.c diff --git a/cipher/caesar_cipher.c b/cipher/caesar_cipher.c new file mode 100644 index 0000000000..3fa9b76680 --- /dev/null +++ b/cipher/caesar_cipher.c @@ -0,0 +1,92 @@ +/** + * @file + * @brief A [caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher) is a + * substitution cipher that replaces each letter in a text by another located + * a given number of positions along the alphabet. + * @author [Andrey Gonçalves Barreto Isidoro](https://github.com/agbi623) + */ + +#include /// for strcmp +#include /// for Input/Output +#include /// for isalpha, isupper and islower +#include /// for assert + +#define MAX_STRING_LENGTH 99 // max length of a string or a char * + +/* + * @brief calculates the encrypted char for the encryption + * @param c the character to be used + * @param key the key to be used + * @returns the encrypted char + */ +int calculate_offset(char c, int key) +{ + int new_c; + + // calculates the offset based on the character's case + if (isupper(c)) + { + new_c = (((int)c - 'A') + key) % 26; + } + else + { + new_c = (((int)c - 'a') + key) % 26; + } + + if (new_c < 0) new_c += 26; + + return isupper(c) ? new_c + 'A' : new_c + 'a'; +} + +/* @brief encrypts the passed text using the passed key integer variable + * @param text the text to be encrypted + * @param key used for shifting a letter, shifts left when negative, right + * when positive + * @returns void + */ +void caesar_cipher(char *text, int key) +{ + for (int i = 0; text[i] != '\0'; i++) + { + if (isalpha(text[i])) text[i] = calculate_offset(text[i], key); + } +} + +/* + * @brief tests the caesar_cipher function by passing some strings to it + * @returns void + */ +void test(void) +{ + char first[] = "ILOVETV"; + char second[] = "ILOVETV"; + char third[] = "The smoke... was blue."; + char fourth[] = "They think they are the fuckin big shot, like the world revolves around them. It\'s funny at times though x\'D"; + + caesar_cipher(first, 15); + assert(strcmp(first, "XADKTIK") == 0); + printf("First test OK\n"); + + caesar_cipher(second, -15); + assert(strcmp(second, "TWZGPEG") == 0); + printf("Second test OK\n"); + + caesar_cipher(third, 35); + assert(strcmp(third, "Cqn bvxtn... fjb kudn.") == 0); + printf("Third test OK\n"); + + caesar_cipher(fourth, -81); + assert(strcmp(fourth, "Qebv qefkh qebv xob qeb crzhfk yfd pelq, ifhb qeb tloia obslisbp xolrka qebj. Fq\'p crkkv xq qfjbp qelrde u\'A") == 0); + printf("Fourth test OK\n"); +} + +/* + * @brief main function; calls the test function + * @returns 0 if all tests pass + */ +int main(void) +{ + test(); + + return 0; +} From 38cdefd9f603e3d547b112bca0bdbcd107856278 Mon Sep 17 00:00:00 2001 From: agbi623 Date: Thu, 3 Sep 2026 23:12:15 -0300 Subject: [PATCH 2/2] docs: clarify documentation --- cipher/caesar_cipher.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/cipher/caesar_cipher.c b/cipher/caesar_cipher.c index 3fa9b76680..579688f685 100644 --- a/cipher/caesar_cipher.c +++ b/cipher/caesar_cipher.c @@ -2,45 +2,39 @@ * @file * @brief A [caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher) is a * substitution cipher that replaces each letter in a text by another located - * a given number of positions along the alphabet. + * a given number of positions along the alphabet. * @author [Andrey Gonçalves Barreto Isidoro](https://github.com/agbi623) */ #include /// for strcmp #include /// for Input/Output -#include /// for isalpha, isupper and islower +#include /// for isalpha, isupper, islower and tolower #include /// for assert -#define MAX_STRING_LENGTH 99 // max length of a string or a char * - -/* +/** * @brief calculates the encrypted char for the encryption * @param c the character to be used * @param key the key to be used * @returns the encrypted char */ -int calculate_offset(char c, int key) +int calculate_encrypted_char(char c, int key) { int new_c; - // calculates the offset based on the character's case - if (isupper(c)) - { - new_c = (((int)c - 'A') + key) % 26; - } - else - { - new_c = (((int)c - 'a') + key) % 26; - } + new_c = ((tolower(c) - 'a') + key) % 26; + // because the % operator sucks if (new_c < 0) new_c += 26; return isupper(c) ? new_c + 'A' : new_c + 'a'; } -/* @brief encrypts the passed text using the passed key integer variable +/** + * @brief encrypts the passed text by calling and assigning the return value + * of calculate_encrypted_char for each alphabetical character in the passed + * text string * @param text the text to be encrypted - * @param key used for shifting a letter, shifts left when negative, right + * @param key used for shifting a letter - shifts left when negative, right * when positive * @returns void */ @@ -48,11 +42,14 @@ void caesar_cipher(char *text, int key) { for (int i = 0; text[i] != '\0'; i++) { - if (isalpha(text[i])) text[i] = calculate_offset(text[i], key); + if (isalpha(text[i])) + { + text[i] = calculate_encrypted_char(text[i], key); + } } } -/* +/** * @brief tests the caesar_cipher function by passing some strings to it * @returns void */ @@ -62,6 +59,7 @@ void test(void) char second[] = "ILOVETV"; char third[] = "The smoke... was blue."; char fourth[] = "They think they are the fuckin big shot, like the world revolves around them. It\'s funny at times though x\'D"; + char fifth[] = "My compass was swallowed by the sea..."; caesar_cipher(first, 15); assert(strcmp(first, "XADKTIK") == 0); @@ -78,9 +76,13 @@ void test(void) caesar_cipher(fourth, -81); assert(strcmp(fourth, "Qebv qefkh qebv xob qeb crzhfk yfd pelq, ifhb qeb tloia obslisbp xolrka qebj. Fq\'p crkkv xq qfjbp qelrde u\'A") == 0); printf("Fourth test OK\n"); + + caesar_cipher(fifth, 0); + assert(strcmp(fifth, "My compass was swallowed by the sea...") == 0); + printf("Fifth test OK\n"); } -/* +/** * @brief main function; calls the test function * @returns 0 if all tests pass */