1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-07-18 02:20:12 +02:00

feat(solutions): add caesar-cipher/c/function

This commit is contained in:
Divlo 2021-10-07 14:51:19 +02:00
parent 3b153a9f1f
commit 108642e57f
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
8 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# caesar-cipher/c/function
Created by [@Divlo](https://github.com/Divlo) on 7 October 2021.

View File

@ -0,0 +1,10 @@
#include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) {
size_t length = strlen(string);
string[length] = character;
string[length + 1] = '\0';
}

View File

@ -0,0 +1,13 @@
#ifndef CHARACTER_H
#define CHARACTER_H
/**
* @brief Append a character to a string, assuming string points to an array
* with enough space.
*
* @param string
* @param character
*/
void character_append(char* string, char character);
#endif

View File

@ -0,0 +1,19 @@
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() {
char character;
size_t length = 1;
char* string = malloc(length * sizeof(char));
*string = '\0';
while ((character = getchar()) != '\n' && character != EOF) {
length++;
string = realloc(string, length * sizeof(char));
character_append(string, character);
}
return string;
}

View File

@ -0,0 +1,11 @@
#ifndef INPUT_H
#define INPUT_H
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
#include "input.h"
#include "string.h"
int main() {
char* string = input();
int shift;
scanf("%d", &shift);
string = string_caesar_cipher(string, shift);
printf("%s\n", string);
free(string);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,43 @@
#include "string.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "character.h"
#define ALPHABET_LENGTH 26
char* string_shift_alphabet(int shift) {
char* result = malloc(sizeof(char) * (ALPHABET_LENGTH + 1));
for (char index = 0; index < ALPHABET_LENGTH; index++) {
char letter = 'A' + index + shift;
if (letter < 'A') {
letter = 'Z' + shift + index + 1;
} else if (letter > 'Z') {
letter = letter - ALPHABET_LENGTH;
}
character_append(result, letter);
}
return result;
}
char* string_caesar_cipher(char* string, int shift) {
size_t string_length = strlen(string);
char* result = malloc(sizeof(char) * (string_length + 1));
char* shifted_alphabet = string_shift_alphabet(shift);
for (size_t index = 0; index < string_length; index++) {
char letter = string[index];
if (letter != ' ') {
for (char index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) {
char current_letter = 'A' + index_alphabet;
if (string[index] == current_letter) {
letter = shifted_alphabet[index_alphabet];
break;
}
}
}
character_append(result, letter);
}
return result;
}

View File

@ -0,0 +1,23 @@
#ifndef STRING_H
#define STRING_H
#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
/**
* @brief Shift the alphabet by a given amount.
*
* @param shift
* @return char*
*/
char* string_shift_alphabet(int shift);
/**
* @brief Encrypts a string using the Caesar cipher.
*
* @param string
* @param shift
* @return char*
*/
char* string_caesar_cipher(char* string, int shift);
#endif