2021-10-07 14:51:19 +02:00
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
#include "character.h"
|
|
|
|
|
2023-08-10 11:13:06 +02:00
|
|
|
size_t string_get_length(const char* string) {
|
|
|
|
size_t length = 0;
|
|
|
|
while (string[length] != '\0') {
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
2021-10-07 14:51:19 +02:00
|
|
|
|
|
|
|
char* string_shift_alphabet(int shift) {
|
|
|
|
char* result = malloc(sizeof(char) * (ALPHABET_LENGTH + 1));
|
2023-08-10 11:13:06 +02:00
|
|
|
for (size_t index = 0; index < ALPHABET_LENGTH; index++) {
|
2021-10-07 14:51:19 +02:00
|
|
|
char letter = 'A' + index + shift;
|
|
|
|
if (letter < 'A') {
|
|
|
|
letter = 'Z' + shift + index + 1;
|
|
|
|
} else if (letter > 'Z') {
|
|
|
|
letter = letter - ALPHABET_LENGTH;
|
|
|
|
}
|
2023-08-10 11:13:06 +02:00
|
|
|
result[index] = letter;
|
2021-10-07 14:51:19 +02:00
|
|
|
}
|
2023-08-10 11:13:06 +02:00
|
|
|
result[ALPHABET_LENGTH] = '\0';
|
2021-10-07 14:51:19 +02:00
|
|
|
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];
|
2023-08-10 11:13:06 +02:00
|
|
|
if (letter != ' ' && (letter >= 'A' && letter <= 'Z')) {
|
|
|
|
for (size_t index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) {
|
2021-10-07 14:51:19 +02:00
|
|
|
char current_letter = 'A' + index_alphabet;
|
|
|
|
if (string[index] == current_letter) {
|
|
|
|
letter = shifted_alphabet[index_alphabet];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 11:13:06 +02:00
|
|
|
result[index] = letter;
|
2021-10-07 14:51:19 +02:00
|
|
|
}
|
2023-08-10 11:13:06 +02:00
|
|
|
result[string_length] = '\0';
|
|
|
|
free(shifted_alphabet);
|
2021-10-07 14:51:19 +02:00
|
|
|
return result;
|
|
|
|
}
|