mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
fix(solutions): memory issues thanks to -fsanitize=address flag with gcc
This commit is contained in:
@ -1,10 +1,15 @@
|
||||
#include "character.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
void character_append(char* string, const char character) {
|
||||
size_t length = string_get_length(string);
|
||||
character_append_at(string, character, length);
|
||||
}
|
||||
|
||||
void character_append(char* string, char character) {
|
||||
size_t length = strlen(string);
|
||||
string[length] = character;
|
||||
void character_append_at(char* string, const char character, const size_t index) {
|
||||
size_t length = string_get_length(string);
|
||||
for (size_t index_string = length; index_string > index; index_string--) {
|
||||
string[index_string] = string[index_string - 1];
|
||||
}
|
||||
string[index] = character;
|
||||
string[length + 1] = '\0';
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
#ifndef __CHARACTER__
|
||||
#define __CHARACTER__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "string.h"
|
||||
|
||||
/**
|
||||
* @brief Append a character to a string, assuming string points to an array
|
||||
* with enough space.
|
||||
@ -10,4 +15,13 @@
|
||||
*/
|
||||
void character_append(char* string, char character);
|
||||
|
||||
/**
|
||||
* @brief Append a character to a string at a specific index, assuming string points to an array with enough space.
|
||||
*
|
||||
* @param string
|
||||
* @param character
|
||||
* @param index
|
||||
*/
|
||||
void character_append_at(char* string, const char character, const size_t index);
|
||||
|
||||
#endif
|
||||
|
@ -1,8 +1,5 @@
|
||||
#include "input.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
char* input() {
|
||||
|
@ -1,6 +1,9 @@
|
||||
#ifndef __INPUT__
|
||||
#define __INPUT__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* @brief Read a line from stdin.
|
||||
*
|
||||
|
@ -9,8 +9,9 @@ int main() {
|
||||
char* string = input();
|
||||
int shift;
|
||||
scanf("%d", &shift);
|
||||
string = string_caesar_cipher(string, shift);
|
||||
printf("%s\n", string);
|
||||
char* result = string_caesar_cipher(string, shift);
|
||||
printf("%s\n", result);
|
||||
free(string);
|
||||
free(result);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -1,24 +1,27 @@
|
||||
#include "string.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
#define ALPHABET_LENGTH 26
|
||||
size_t string_get_length(const char* string) {
|
||||
size_t length = 0;
|
||||
while (string[length] != '\0') {
|
||||
length++;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
char* string_shift_alphabet(int shift) {
|
||||
char* result = malloc(sizeof(char) * (ALPHABET_LENGTH + 1));
|
||||
for (char index = 0; index < ALPHABET_LENGTH; index++) {
|
||||
for (size_t 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);
|
||||
result[index] = letter;
|
||||
}
|
||||
result[ALPHABET_LENGTH] = '\0';
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -28,8 +31,8 @@ char* string_caesar_cipher(char* string, int shift) {
|
||||
char* shifted_alphabet = string_shift_alphabet(shift);
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
char letter = string[index];
|
||||
if (letter != ' ') {
|
||||
for (int index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) {
|
||||
if (letter != ' ' && (letter >= 'A' && letter <= 'Z')) {
|
||||
for (size_t 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];
|
||||
@ -37,7 +40,9 @@ char* string_caesar_cipher(char* string, int shift) {
|
||||
}
|
||||
}
|
||||
}
|
||||
character_append(result, letter);
|
||||
result[index] = letter;
|
||||
}
|
||||
result[string_length] = '\0';
|
||||
free(shifted_alphabet);
|
||||
return result;
|
||||
}
|
||||
|
@ -1,8 +1,20 @@
|
||||
#ifndef __STRING__
|
||||
#define __STRING__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ALPHABET_LENGTH 26
|
||||
#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
/**
|
||||
* @brief Return the length of a string (excluding '\0').
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
size_t string_get_length(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Shift the alphabet by a given amount.
|
||||
*
|
||||
|
Reference in New Issue
Block a user