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,8 +1,5 @@
|
||||
#include "character.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void character_append(char* string, char character) {
|
||||
size_t length = strlen(string);
|
||||
string[length] = character;
|
||||
|
@ -1,6 +1,9 @@
|
||||
#ifndef __CHARACTER__
|
||||
#define __CHARACTER__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @brief Append a character to a string, assuming string points to an array
|
||||
* with enough space.
|
||||
|
@ -1,10 +1,5 @@
|
||||
#include "input.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
char* input() {
|
||||
char character;
|
||||
size_t length = 1;
|
||||
|
@ -1,6 +1,11 @@
|
||||
#ifndef __INPUT__
|
||||
#define __INPUT__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
/**
|
||||
* @brief Read a line from stdin.
|
||||
*
|
||||
|
@ -9,8 +9,8 @@
|
||||
|
||||
int main() {
|
||||
char* string = input();
|
||||
string = string_to_upper(string);
|
||||
string = string_replace(string, ' ', '\0');
|
||||
string_to_uppercase(string);
|
||||
string_remove_character(string, ' ');
|
||||
bool is_palindrome = string_is_palindrome(string);
|
||||
free(string);
|
||||
printf("%s\n", is_palindrome ? "true" : "false");
|
||||
|
@ -1,46 +1,67 @@
|
||||
#include "string.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
size_t string_get_length(const char* string) {
|
||||
size_t length = 0;
|
||||
while (string[length] != '\0') {
|
||||
length++;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
#include "character.h"
|
||||
|
||||
char* string_to_upper(const char* string) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
void string_to_uppercase(char* string) {
|
||||
size_t string_length = string_get_length(string);
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
character_append(result, character_to_upper(string[index]));
|
||||
string[index] = character_to_upper(string[index]);
|
||||
}
|
||||
return result;
|
||||
string[string_length] = '\0';
|
||||
}
|
||||
|
||||
char* string_reverse(const char* string) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
for (int index = string_length - 1; index != -1; index--) {
|
||||
character_append(result, string[index]);
|
||||
void string_remove_character(char* string, char search) {
|
||||
size_t string_length = string_get_length(string);
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
if (string[index] == search) {
|
||||
for (size_t index_string = index; index_string < string_length; index_string++) {
|
||||
string[index_string] = string[index_string + 1];
|
||||
}
|
||||
string_length--;
|
||||
index--;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
string[string_length] = '\0';
|
||||
}
|
||||
|
||||
bool string_is_palindrome(const char* string) {
|
||||
char* string_reversed = string_reverse(string);
|
||||
bool is_palindrome = strcmp(string_reversed, string) == 0;
|
||||
char* string_copy(const char* string) {
|
||||
size_t source_length = string_get_length(string);
|
||||
char* copy = malloc(sizeof(char) * (source_length + 1));
|
||||
if (copy == NULL) {
|
||||
perror("Error (string_copy)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
size_t index;
|
||||
for (index = 0; index < source_length; index++) {
|
||||
copy[index] = string[index];
|
||||
}
|
||||
copy[index] = '\0';
|
||||
return copy;
|
||||
}
|
||||
|
||||
void string_reverse(char* string) {
|
||||
size_t string_length = string_get_length(string);
|
||||
size_t index_start = 0;
|
||||
size_t index_end = string_length - 1;
|
||||
while (index_start < index_end) {
|
||||
char temporary = string[index_start];
|
||||
string[index_start] = string[index_end];
|
||||
string[index_end] = temporary;
|
||||
index_start++;
|
||||
index_end--;
|
||||
}
|
||||
}
|
||||
|
||||
bool string_is_palindrome(char* string) {
|
||||
char* string_reversed = string_copy(string);
|
||||
string_reverse(string_reversed);
|
||||
bool is_palindrome = strcmp(string, string_reversed) == 0;
|
||||
free(string_reversed);
|
||||
return is_palindrome;
|
||||
}
|
||||
|
||||
char* string_replace(const char* string, char search, char replace) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
bool is_search_value = search == string[index];
|
||||
if (is_search_value) {
|
||||
character_append(result, replace);
|
||||
} else {
|
||||
character_append(result, string[index]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -1,23 +1,57 @@
|
||||
#ifndef __STRING__
|
||||
#define __STRING__
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
/**
|
||||
* @brief Return the length of a string (excluding '\0').
|
||||
*
|
||||
* @param string
|
||||
* @return size_t
|
||||
*/
|
||||
size_t string_get_length(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Converts all the alphabetic characters in a string to uppercase.
|
||||
*
|
||||
* NOTE: Mutates the string.
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
*/
|
||||
char* string_to_upper(const char* string);
|
||||
void string_to_uppercase(char* string);
|
||||
|
||||
/**
|
||||
* @brief Reverse the characters in an array.
|
||||
* @brief Removes all the occurrences of a character in a string.
|
||||
*
|
||||
* NOTE: Mutates the string.
|
||||
*
|
||||
* @param string
|
||||
* @param search A character search value.
|
||||
*/
|
||||
void string_remove_character(char* string, char search);
|
||||
|
||||
/**
|
||||
* @brief Reverse the characters in a string.
|
||||
*
|
||||
* NOTE: Mutates the string.
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
void string_reverse(char* string);
|
||||
|
||||
/**
|
||||
* @brief Return the copy of a string.
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
*/
|
||||
char* string_reverse(const char* string);
|
||||
char* string_copy(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Returns true if the string is a palindrome (a palindrome is a word,
|
||||
@ -27,17 +61,6 @@ char* string_reverse(const char* string);
|
||||
* @param string The string to check.
|
||||
* @return true if the string is a palindrome, false otherwise.
|
||||
*/
|
||||
bool string_is_palindrome(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Replace all the occurrences of search value into replace value in
|
||||
* the string.
|
||||
*
|
||||
* @param string
|
||||
* @param search_value A character search value.
|
||||
* @param replace_value A character containing the text to replace for match.
|
||||
* @return char*
|
||||
*/
|
||||
char* string_replace(const char* string, char search, char replace);
|
||||
bool string_is_palindrome(char* string);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user