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,11 +1,16 @@
|
||||
#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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "string.h"
|
||||
|
||||
/**
|
||||
* @brief Append a character to a string, assuming string points to an array
|
||||
* with enough space.
|
||||
@ -10,6 +15,15 @@
|
||||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* @brief Converts the character to uppercase.
|
||||
*
|
||||
|
@ -7,10 +7,11 @@
|
||||
|
||||
int main() {
|
||||
char* string = input();
|
||||
string = string_replace(string, '"', '\0');
|
||||
string = string_to_upper(string);
|
||||
string = string_acronym(string);
|
||||
printf("%s\n", string);
|
||||
string_remove_character(string, '"');
|
||||
string_to_uppercase(string);
|
||||
char* result = string_acronym(string);
|
||||
printf("%s\n", result);
|
||||
free(string);
|
||||
free(result);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -1,47 +1,55 @@
|
||||
#include "string.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
char* string_to_upper(const char* string) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
character_append(result, character_to_upper(string[index]));
|
||||
size_t string_get_length(const char* string) {
|
||||
size_t length = 0;
|
||||
while (string[length] != '\0') {
|
||||
length++;
|
||||
}
|
||||
return result;
|
||||
return length;
|
||||
}
|
||||
|
||||
char* string_replace(const char* string, char search, char replace) {
|
||||
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++) {
|
||||
bool is_search_value = search == string[index];
|
||||
if (is_search_value) {
|
||||
character_append(result, replace);
|
||||
} else {
|
||||
character_append(result, string[index]);
|
||||
string[index] = character_to_upper(string[index]);
|
||||
}
|
||||
string[string_length] = '\0';
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
char* string_acronym(char* string) {
|
||||
size_t string_length = strlen(string);
|
||||
size_t string_length = string_get_length(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
char* current = malloc(sizeof(char) * (string_length + 1));
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
if (string[index] == ' ') {
|
||||
character_append(result, current[0]);
|
||||
memset(current, 0, sizeof(char) * (string_length + 1));
|
||||
} else {
|
||||
character_append(current, string[index]);
|
||||
char current = '\0';
|
||||
size_t result_index = 0;
|
||||
bool is_first_character = true;
|
||||
for (size_t string_index = 0; string_index < string_length; string_index++) {
|
||||
if (string[string_index] == ' ') {
|
||||
result[result_index] = current;
|
||||
result_index += 1;
|
||||
is_first_character = true;
|
||||
} else if (is_first_character) {
|
||||
current = string[string_index];
|
||||
is_first_character = false;
|
||||
}
|
||||
}
|
||||
character_append(result, current[0]);
|
||||
free(current);
|
||||
result[result_index] = current;
|
||||
result_index += 1;
|
||||
result[result_index] = '\0';
|
||||
return result;
|
||||
}
|
||||
|
@ -1,24 +1,37 @@
|
||||
#ifndef __STRING__
|
||||
#define __STRING__
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @brief Return the length of a string (excluding '\0').
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
size_t string_get_length(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Converts all the alphabetic characters in a string to uppercase.
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
*/
|
||||
char* string_to_upper(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Replace all the occurrences of search value into replace value in
|
||||
* the string.
|
||||
* NOTE: Mutates 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);
|
||||
void string_to_uppercase(char* string);
|
||||
|
||||
/**
|
||||
* @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 Converts a string to its acronym.
|
||||
|
Reference in New Issue
Block a user