mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add caesar-cipher/c/function
This commit is contained in:
parent
3b153a9f1f
commit
108642e57f
3
challenges/caesar-cipher/solutions/c/function/README.md
Normal file
3
challenges/caesar-cipher/solutions/c/function/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# caesar-cipher/c/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 7 October 2021.
|
10
challenges/caesar-cipher/solutions/c/function/character.c
Normal file
10
challenges/caesar-cipher/solutions/c/function/character.c
Normal 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';
|
||||
}
|
13
challenges/caesar-cipher/solutions/c/function/character.h
Normal file
13
challenges/caesar-cipher/solutions/c/function/character.h
Normal 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
|
19
challenges/caesar-cipher/solutions/c/function/input.c
Normal file
19
challenges/caesar-cipher/solutions/c/function/input.c
Normal 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;
|
||||
}
|
11
challenges/caesar-cipher/solutions/c/function/input.h
Normal file
11
challenges/caesar-cipher/solutions/c/function/input.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef INPUT_H
|
||||
#define INPUT_H
|
||||
|
||||
/**
|
||||
* @brief Read a line from stdin.
|
||||
*
|
||||
* @return char*
|
||||
*/
|
||||
char* input();
|
||||
|
||||
#endif
|
16
challenges/caesar-cipher/solutions/c/function/solution.c
Normal file
16
challenges/caesar-cipher/solutions/c/function/solution.c
Normal 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;
|
||||
}
|
43
challenges/caesar-cipher/solutions/c/function/string.c
Normal file
43
challenges/caesar-cipher/solutions/c/function/string.c
Normal 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;
|
||||
}
|
23
challenges/caesar-cipher/solutions/c/function/string.h
Normal file
23
challenges/caesar-cipher/solutions/c/function/string.h
Normal 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
|
Loading…
Reference in New Issue
Block a user