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,9 +1,5 @@
|
||||
#include "character.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void character_append(char* string, char character) {
|
||||
size_t length = strlen(string);
|
||||
string[length] = character;
|
||||
|
@ -2,6 +2,8 @@
|
||||
#define __CHARACTER__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @brief Append a character to a string, assuming string points to an array
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -7,11 +7,12 @@
|
||||
|
||||
int main() {
|
||||
char* string = input();
|
||||
string = string_trim(string, ' ');
|
||||
string = string_trim(string, '-');
|
||||
string = string_to_lowercase(string);
|
||||
string = string_slugify(string);
|
||||
printf("%s\n", string);
|
||||
string_trim(string, ' ');
|
||||
string_trim(string, '-');
|
||||
string_to_lowercase(string);
|
||||
char* result = string_slugify(string);
|
||||
printf("%s\n", result);
|
||||
free(string);
|
||||
free(result);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -1,66 +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"
|
||||
void string_to_lowercase(char* string) {
|
||||
size_t string_length = string_get_length(string);
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
string[index] = character_to_lower(string[index]);
|
||||
}
|
||||
string[string_length] = '\0';
|
||||
}
|
||||
|
||||
char* string_trim_start(char* string, char character) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
void string_trim_start(char* string, char character) {
|
||||
size_t string_length = string_get_length(string);
|
||||
size_t index_space = 0;
|
||||
while (string[index_space] == character) {
|
||||
index_space++;
|
||||
}
|
||||
for (size_t index = index_space; index < string_length; index++) {
|
||||
character_append(result, string[index]);
|
||||
for (size_t index = 0; index < string_length - index_space; index++) {
|
||||
string[index] = string[index + index_space];
|
||||
}
|
||||
return result;
|
||||
string[string_length - index_space] = '\0';
|
||||
}
|
||||
|
||||
char* string_trim_end(char* string, char character) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
void string_trim_end(char* string, char character) {
|
||||
size_t string_length = string_get_length(string);
|
||||
size_t index_space = string_length - 1;
|
||||
while (string[index_space] == character) {
|
||||
index_space--;
|
||||
}
|
||||
for (size_t index = 0; index < index_space + 1; index++) {
|
||||
character_append(result, string[index]);
|
||||
}
|
||||
return result;
|
||||
string[index_space + 1] = '\0';
|
||||
}
|
||||
|
||||
char* string_trim(char* string, char character) {
|
||||
char* result = string_trim_start(string, character);
|
||||
result = string_trim_end(result, character);
|
||||
return result;
|
||||
}
|
||||
|
||||
char* string_to_lowercase(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_lower(string[index]));
|
||||
}
|
||||
return result;
|
||||
void string_trim(char* string, char character) {
|
||||
string_trim_start(string, character);
|
||||
string_trim_end(string, character);
|
||||
}
|
||||
|
||||
char* string_slugify(char* string) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
int words = 0;
|
||||
result[0] = '\0';
|
||||
size_t words = 0;
|
||||
char* current = malloc(sizeof(char) * (string_length + 1));
|
||||
current[0] = '\0';
|
||||
size_t current_index = 0;
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
if (string[index] == ' ' || (string[index] == '-' && strlen(current) > 0)) {
|
||||
strcat(result, current);
|
||||
character_append(result, '-');
|
||||
memset(current, 0, sizeof(char) * (string_length + 1));
|
||||
words++;
|
||||
current_index = 0;
|
||||
words += 1;
|
||||
} else {
|
||||
if (character_is_alphanumeric(string[index])) {
|
||||
character_append(current, string[index]);
|
||||
current[current_index] = string[index];
|
||||
current_index += 1;
|
||||
current[current_index] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,55 @@
|
||||
#ifndef __STRING__
|
||||
#define __STRING__
|
||||
|
||||
/**
|
||||
* @brief Removes all `character` from the start of a string.
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
*/
|
||||
char* string_trim_start(char* string, char character);
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
/**
|
||||
* @brief Removes all `character` from the end of a string.
|
||||
* @brief Return the length of a string (excluding '\0').
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
* @return size_t
|
||||
*/
|
||||
char* string_trim_end(char* string, char character);
|
||||
|
||||
/**
|
||||
* @brief Removes all `character` from the start and end of a string.
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
*/
|
||||
char* string_trim(char* string, char character);
|
||||
size_t string_get_length(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Converts all the alphabetic characters in a string to lowercase.
|
||||
*
|
||||
* NOTE: Mutates the string.
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
*/
|
||||
char* string_to_lowercase(char* string);
|
||||
void string_to_lowercase(char* string);
|
||||
|
||||
/**
|
||||
* @brief Removes all `character` from the start of a string.
|
||||
*
|
||||
* NOTE: Mutates the string.
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
void string_trim_start(char* string, char character);
|
||||
|
||||
/**
|
||||
* @brief Removes all `character` from the end of a string.
|
||||
*
|
||||
* NOTE: Mutates the string.
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
void string_trim_end(char* string, char character);
|
||||
|
||||
/**
|
||||
* @brief Removes all `character` from the start and end of a string.
|
||||
*
|
||||
* NOTE: Mutates the string.
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
void string_trim(char* string, char character);
|
||||
|
||||
/**
|
||||
* @brief Generate a slug from a string.
|
||||
|
Reference in New Issue
Block a user