diff --git a/challenges/acronyms/solutions/c/function/character.c b/challenges/acronyms/solutions/c/function/character.c index 76f56ea..b240d21 100644 --- a/challenges/acronyms/solutions/c/function/character.c +++ b/challenges/acronyms/solutions/c/function/character.c @@ -1,11 +1,16 @@ #include "character.h" -#include -#include +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'; } diff --git a/challenges/acronyms/solutions/c/function/character.h b/challenges/acronyms/solutions/c/function/character.h index b49dd7a..3afc0f1 100644 --- a/challenges/acronyms/solutions/c/function/character.h +++ b/challenges/acronyms/solutions/c/function/character.h @@ -1,6 +1,11 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + +#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. * diff --git a/challenges/acronyms/solutions/c/function/solution.c b/challenges/acronyms/solutions/c/function/solution.c index 4ac7b60..117842c 100644 --- a/challenges/acronyms/solutions/c/function/solution.c +++ b/challenges/acronyms/solutions/c/function/solution.c @@ -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; } diff --git a/challenges/acronyms/solutions/c/function/string.c b/challenges/acronyms/solutions/c/function/string.c index e77fd2d..4fea7c2 100644 --- a/challenges/acronyms/solutions/c/function/string.c +++ b/challenges/acronyms/solutions/c/function/string.c @@ -1,47 +1,55 @@ #include "string.h" -#include -#include -#include - #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; } diff --git a/challenges/acronyms/solutions/c/function/string.h b/challenges/acronyms/solutions/c/function/string.h index 11ad7e9..74acc2d 100644 --- a/challenges/acronyms/solutions/c/function/string.h +++ b/challenges/acronyms/solutions/c/function/string.h @@ -1,24 +1,37 @@ #ifndef __STRING__ #define __STRING__ +#include +#include +#include +#include +#include + +/** + * @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. diff --git a/challenges/caesar-cipher/solutions/c/function/character.c b/challenges/caesar-cipher/solutions/c/function/character.c index 49d4f00..0b825e3 100644 --- a/challenges/caesar-cipher/solutions/c/function/character.c +++ b/challenges/caesar-cipher/solutions/c/function/character.c @@ -1,10 +1,15 @@ #include "character.h" -#include -#include +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'; } diff --git a/challenges/caesar-cipher/solutions/c/function/character.h b/challenges/caesar-cipher/solutions/c/function/character.h index 0fab88f..3fe738e 100644 --- a/challenges/caesar-cipher/solutions/c/function/character.h +++ b/challenges/caesar-cipher/solutions/c/function/character.h @@ -1,6 +1,11 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + +#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 diff --git a/challenges/caesar-cipher/solutions/c/function/input.c b/challenges/caesar-cipher/solutions/c/function/input.c index c07bb6d..df9ee32 100644 --- a/challenges/caesar-cipher/solutions/c/function/input.c +++ b/challenges/caesar-cipher/solutions/c/function/input.c @@ -1,8 +1,5 @@ #include "input.h" -#include -#include - #include "character.h" char* input() { diff --git a/challenges/caesar-cipher/solutions/c/function/input.h b/challenges/caesar-cipher/solutions/c/function/input.h index d432398..d7e885d 100644 --- a/challenges/caesar-cipher/solutions/c/function/input.h +++ b/challenges/caesar-cipher/solutions/c/function/input.h @@ -1,6 +1,9 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + /** * @brief Read a line from stdin. * diff --git a/challenges/caesar-cipher/solutions/c/function/solution.c b/challenges/caesar-cipher/solutions/c/function/solution.c index 89bd9e2..8d8e944 100644 --- a/challenges/caesar-cipher/solutions/c/function/solution.c +++ b/challenges/caesar-cipher/solutions/c/function/solution.c @@ -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; } diff --git a/challenges/caesar-cipher/solutions/c/function/string.c b/challenges/caesar-cipher/solutions/c/function/string.c index 7dd73fe..8e89338 100644 --- a/challenges/caesar-cipher/solutions/c/function/string.c +++ b/challenges/caesar-cipher/solutions/c/function/string.c @@ -1,24 +1,27 @@ #include "string.h" -#include -#include -#include - #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; } diff --git a/challenges/caesar-cipher/solutions/c/function/string.h b/challenges/caesar-cipher/solutions/c/function/string.h index d775145..3e13457 100644 --- a/challenges/caesar-cipher/solutions/c/function/string.h +++ b/challenges/caesar-cipher/solutions/c/function/string.h @@ -1,8 +1,20 @@ #ifndef __STRING__ #define __STRING__ +#include +#include +#include + +#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. * diff --git a/challenges/camel-case/solutions/c/function/character.c b/challenges/camel-case/solutions/c/function/character.c index 76f56ea..08d49b9 100644 --- a/challenges/camel-case/solutions/c/function/character.c +++ b/challenges/camel-case/solutions/c/function/character.c @@ -1,20 +1,22 @@ #include "character.h" -#include -#include +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'; } -char character_to_upper(char character) { - char ascii_a = 'a'; - char ascii_A = 'A'; - char ascii_z = 'z'; - if (character >= ascii_a && character <= ascii_z) { - return character + (ascii_A - ascii_a); +char character_to_upper(const char character) { + if (character >= 'a' && character <= 'z') { + return character + ('A' - 'a'); } return character; } diff --git a/challenges/camel-case/solutions/c/function/character.h b/challenges/camel-case/solutions/c/function/character.h index b49dd7a..b245b20 100644 --- a/challenges/camel-case/solutions/c/function/character.h +++ b/challenges/camel-case/solutions/c/function/character.h @@ -1,6 +1,12 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include +#include + +#include "string.h" + /** * @brief Append a character to a string, assuming string points to an array * with enough space. @@ -10,12 +16,20 @@ */ 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. * * @param character - * @return char */ -char character_to_upper(char character); +char character_to_upper(const char character); #endif diff --git a/challenges/camel-case/solutions/c/function/input.c b/challenges/camel-case/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/camel-case/solutions/c/function/input.c +++ b/challenges/camel-case/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/camel-case/solutions/c/function/input.h b/challenges/camel-case/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/camel-case/solutions/c/function/input.h +++ b/challenges/camel-case/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/camel-case/solutions/c/function/solution.c b/challenges/camel-case/solutions/c/function/solution.c index be9aab3..0b9e406 100644 --- a/challenges/camel-case/solutions/c/function/solution.c +++ b/challenges/camel-case/solutions/c/function/solution.c @@ -7,9 +7,10 @@ int main() { char* string = input(); - string = string_trim(string); - string = string_camelCase(string); - printf("%s\n", string); + string_trim(string, ' '); + char* result = string_camelCase(string); + printf("%s\n", result); free(string); + free(result); return EXIT_SUCCESS; } diff --git a/challenges/camel-case/solutions/c/function/string.c b/challenges/camel-case/solutions/c/function/string.c index 58cb61e..2c1705f 100644 --- a/challenges/camel-case/solutions/c/function/string.c +++ b/challenges/camel-case/solutions/c/function/string.c @@ -1,66 +1,73 @@ #include "string.h" -#include -#include -#include +size_t string_get_length(const char* string) { + size_t length = 0; + while (string[length] != '\0') { + length++; + } + return length; +} -#include "character.h" - -char* string_trim_start(char* string) { - 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] == ' ') { + 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) { - 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] == ' ') { + 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* result = string_trim_start(string); - result = string_trim_end(result); - return result; +void string_trim(char* string, char character) { + string_trim_start(string, character); + string_trim_end(string, character); } -char* string_capitalize(char* string) { - size_t string_length = strlen(string); - if (string_length > 0) { - string[0] = character_to_upper(string[0]); +void string_capitalize(char* string) { + size_t string_length = string_get_length(string); + if (string_length == 0) { + return; } - return string; + string[0] = character_to_upper(string[0]); } char* string_camelCase(char* string) { - size_t string_length = strlen(string); + size_t string_length = string_get_length(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)); + size_t current_index = 0; for (size_t index = 0; index < string_length; index++) { if (string[index] == ' ') { - strcat(result, words == 0 ? current : string_capitalize(current)); + current[current_index] = '\0'; + if (words > 0) { + string_capitalize(current); + } + strcat(result, current); memset(current, 0, sizeof(char) * (string_length + 1)); - words++; + current_index = 0; + words += 1; } else { - character_append(current, string[index]); + current[current_index] = string[index]; + current_index += 1; } } - strcat(result, words == 0 ? current : string_capitalize(current)); + if (words > 0) { + string_capitalize(current); + strcat(result, current); + } free(current); return result; } diff --git a/challenges/camel-case/solutions/c/function/string.h b/challenges/camel-case/solutions/c/function/string.h index 7584bd1..1c6bd44 100644 --- a/challenges/camel-case/solutions/c/function/string.h +++ b/challenges/camel-case/solutions/c/function/string.h @@ -1,29 +1,46 @@ #ifndef __STRING__ #define __STRING__ -/** - * @brief Removes all whitespace from the start of a string. - * - * @param string - * @return char* - */ -char* string_trim_start(char* string); +#include +#include +#include + +#include "character.h" /** - * @brief Removes all whitespace 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); +size_t string_get_length(const char* string); /** - * @brief Removes all whitespace from the start and end of a string. + * @brief Removes all `character` from the start of a string. + * + * NOTE: Mutates the string. * * @param string - * @return char* */ -char* string_trim(char* 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 Converts a string to camel case. @@ -36,9 +53,10 @@ char* string_camelCase(char* string); /** * @brief Capitalizes the string. * + * NOTE: Mutates the string. + * * @param string - * @return char* */ -char* string_capitalize(char* string); +void string_capitalize(char* string); #endif diff --git a/challenges/consecutive-numbers/solutions/c/function/character.c b/challenges/consecutive-numbers/solutions/c/function/character.c index 49d4f00..3e8c0ff 100644 --- a/challenges/consecutive-numbers/solutions/c/function/character.c +++ b/challenges/consecutive-numbers/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/consecutive-numbers/solutions/c/function/character.h b/challenges/consecutive-numbers/solutions/c/function/character.h index 0fab88f..e3a2037 100644 --- a/challenges/consecutive-numbers/solutions/c/function/character.h +++ b/challenges/consecutive-numbers/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/consecutive-numbers/solutions/c/function/input.c b/challenges/consecutive-numbers/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/consecutive-numbers/solutions/c/function/input.c +++ b/challenges/consecutive-numbers/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/consecutive-numbers/solutions/c/function/input.h b/challenges/consecutive-numbers/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/consecutive-numbers/solutions/c/function/input.h +++ b/challenges/consecutive-numbers/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/character.c b/challenges/convert-number-from-base-to-another/solutions/c/function/character.c index 49d4f00..3e8c0ff 100644 --- a/challenges/convert-number-from-base-to-another/solutions/c/function/character.c +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/character.h b/challenges/convert-number-from-base-to-another/solutions/c/function/character.h index 0fab88f..e3a2037 100644 --- a/challenges/convert-number-from-base-to-another/solutions/c/function/character.h +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/input.c b/challenges/convert-number-from-base-to-another/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/convert-number-from-base-to-another/solutions/c/function/input.c +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/input.h b/challenges/convert-number-from-base-to-another/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/convert-number-from-base-to-another/solutions/c/function/input.h +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/convert-number-from-base-to-another/solutions/c/function/solution.c b/challenges/convert-number-from-base-to-another/solutions/c/function/solution.c index 92746e3..bbbcdec 100644 --- a/challenges/convert-number-from-base-to-another/solutions/c/function/solution.c +++ b/challenges/convert-number-from-base-to-another/solutions/c/function/solution.c @@ -4,15 +4,11 @@ #include "input.h" -int math_power(int base, int power) { - int result = 1; - for (int iteration = 0; iteration < power; iteration++) { - result = result * base; - } - return result; +unsigned long long mathematics_pow(unsigned long long base, unsigned long long exponent) { + return exponent == 0 ? 1 : base * mathematics_pow(base, exponent - 1); } -char* convert_from_base_10_to_base(unsigned long number, unsigned int base) { +char* convert_number_from_base_10_to_base(unsigned long long number, unsigned long base) { if (number == 0) { return "0"; } @@ -34,13 +30,14 @@ char* convert_from_base_10_to_base(unsigned long number, unsigned int base) { } index_result++; } + result[index_result] = '\0'; return result; } -int convert_from_base_to_base_10(char* number, unsigned int base) { - int length = strlen(number); +unsigned long convert_number_from_base_to_base_10(char* number, unsigned long base) { + size_t length = strlen(number); int exponent = length - 1; - int result = 0; + unsigned long result = 0; int index = 0; while (exponent >= 0) { int current_number = (int)(number[index] - '0'); @@ -49,19 +46,26 @@ int convert_from_base_to_base_10(char* number, unsigned int base) { } else { current_number = (int)(number[index] - '0'); } - result = result + current_number * math_power(base, exponent); + result = result + current_number * mathematics_pow(base, exponent); exponent--; index++; } return result; } +char* convert_number_from_base_to_another(char* number, unsigned long base_from, unsigned long base_target) { + return convert_number_from_base_10_to_base(convert_number_from_base_to_base_10(number, base_from), base_target); +} + int main() { char* number = input(); - int base_from; - int base_target; - scanf("%d", &base_from); - scanf("%d", &base_target); - printf("%s\n", convert_from_base_10_to_base(convert_from_base_to_base_10(number, base_from), base_target)); + unsigned long base_from; + unsigned long base_target; + scanf("%lu", &base_from); + scanf("%lu", &base_target); + char* result = convert_number_from_base_to_another(number, base_from, base_target); + printf("%s\n", result); + free(number); + free(result); return EXIT_SUCCESS; } diff --git a/challenges/fibonacci/solutions/c/function/solution.c b/challenges/fibonacci/solutions/c/function/solution.c index 403c119..a77a83d 100644 --- a/challenges/fibonacci/solutions/c/function/solution.c +++ b/challenges/fibonacci/solutions/c/function/solution.c @@ -1,13 +1,13 @@ #include #include -int fibonacci(int number) { +unsigned long fibonacci(unsigned long number) { return number < 2 ? number : fibonacci(number - 1) + fibonacci(number - 2); } int main() { - int number; - scanf("%d", &number); - printf("%d\n", fibonacci(number)); + unsigned long number; + scanf("%lu", &number); + printf("%lu\n", fibonacci(number)); return EXIT_SUCCESS; } diff --git a/challenges/first-non-repeating-character/solutions/c/function/character.c b/challenges/first-non-repeating-character/solutions/c/function/character.c index 49d4f00..3e8c0ff 100644 --- a/challenges/first-non-repeating-character/solutions/c/function/character.c +++ b/challenges/first-non-repeating-character/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/first-non-repeating-character/solutions/c/function/character.h b/challenges/first-non-repeating-character/solutions/c/function/character.h index 0fab88f..e3a2037 100644 --- a/challenges/first-non-repeating-character/solutions/c/function/character.h +++ b/challenges/first-non-repeating-character/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/first-non-repeating-character/solutions/c/function/input.c b/challenges/first-non-repeating-character/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/first-non-repeating-character/solutions/c/function/input.c +++ b/challenges/first-non-repeating-character/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/first-non-repeating-character/solutions/c/function/input.h b/challenges/first-non-repeating-character/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/first-non-repeating-character/solutions/c/function/input.h +++ b/challenges/first-non-repeating-character/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/first-non-repeating-character/solutions/c/function/string.c b/challenges/first-non-repeating-character/solutions/c/function/string.c index 93a620c..5637d5b 100644 --- a/challenges/first-non-repeating-character/solutions/c/function/string.c +++ b/challenges/first-non-repeating-character/solutions/c/function/string.c @@ -1,11 +1,5 @@ #include "string.h" -#include -#include -#include - -#include "character.h" - size_t string_total_occurrences_of_character(char* string, char character) { size_t result = 0; size_t string_length = strlen(string); diff --git a/challenges/first-non-repeating-character/solutions/c/function/string.h b/challenges/first-non-repeating-character/solutions/c/function/string.h index 86d85b6..f77987c 100644 --- a/challenges/first-non-repeating-character/solutions/c/function/string.h +++ b/challenges/first-non-repeating-character/solutions/c/function/string.h @@ -1,7 +1,11 @@ #ifndef __STRING__ #define __STRING__ +#include #include +#include + +#include "character.h" /** * @brief Returns the total number of occurrences of the given character in the string. diff --git a/challenges/fizzbuzz/solutions/c/function/solution.c b/challenges/fizzbuzz/solutions/c/function/solution.c index 75e3afd..34ecc41 100644 --- a/challenges/fizzbuzz/solutions/c/function/solution.c +++ b/challenges/fizzbuzz/solutions/c/function/solution.c @@ -3,9 +3,9 @@ #include int main() { - int length; - scanf("%d", &length); - for (int number = 1; number <= length; number++) { + unsigned long length; + scanf("%lu", &length); + for (unsigned long number = 1; number <= length; number++) { bool is_divisible_by_3 = number % 3 == 0; bool is_divisible_by_5 = number % 5 == 0; if (is_divisible_by_3 && is_divisible_by_5) { @@ -15,7 +15,7 @@ int main() { } else if (is_divisible_by_5) { printf("Buzz\n"); } else { - printf("%d\n", number); + printf("%lu\n", number); } } return EXIT_SUCCESS; diff --git a/challenges/heap-algorithm/solutions/c/function/character.c b/challenges/heap-algorithm/solutions/c/function/character.c index 49d4f00..3e8c0ff 100644 --- a/challenges/heap-algorithm/solutions/c/function/character.c +++ b/challenges/heap-algorithm/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/heap-algorithm/solutions/c/function/character.h b/challenges/heap-algorithm/solutions/c/function/character.h index 0fab88f..e3a2037 100644 --- a/challenges/heap-algorithm/solutions/c/function/character.h +++ b/challenges/heap-algorithm/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/heap-algorithm/solutions/c/function/input.c b/challenges/heap-algorithm/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/heap-algorithm/solutions/c/function/input.c +++ b/challenges/heap-algorithm/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/heap-algorithm/solutions/c/function/input.h b/challenges/heap-algorithm/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/heap-algorithm/solutions/c/function/input.h +++ b/challenges/heap-algorithm/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/hello-world/solutions/c/function/character.c b/challenges/hello-world/solutions/c/function/character.c index 49d4f00..3e8c0ff 100644 --- a/challenges/hello-world/solutions/c/function/character.c +++ b/challenges/hello-world/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/hello-world/solutions/c/function/character.h b/challenges/hello-world/solutions/c/function/character.h index 0fab88f..e3a2037 100644 --- a/challenges/hello-world/solutions/c/function/character.h +++ b/challenges/hello-world/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/hello-world/solutions/c/function/input.c b/challenges/hello-world/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/hello-world/solutions/c/function/input.c +++ b/challenges/hello-world/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/hello-world/solutions/c/function/input.h b/challenges/hello-world/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/hello-world/solutions/c/function/input.h +++ b/challenges/hello-world/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/hello-world/solutions/c/function/solution.c b/challenges/hello-world/solutions/c/function/solution.c index 30ce41e..7e0f37f 100644 --- a/challenges/hello-world/solutions/c/function/solution.c +++ b/challenges/hello-world/solutions/c/function/solution.c @@ -6,5 +6,6 @@ int main() { char *string = input(); printf("Hello, %s!\n", string); + free(string); return EXIT_SUCCESS; } diff --git a/challenges/is-palindrome/solutions/c/function/character.c b/challenges/is-palindrome/solutions/c/function/character.c index 76f56ea..a760202 100644 --- a/challenges/is-palindrome/solutions/c/function/character.c +++ b/challenges/is-palindrome/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/is-palindrome/solutions/c/function/character.h b/challenges/is-palindrome/solutions/c/function/character.h index b49dd7a..1435141 100644 --- a/challenges/is-palindrome/solutions/c/function/character.h +++ b/challenges/is-palindrome/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/is-palindrome/solutions/c/function/input.c b/challenges/is-palindrome/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/is-palindrome/solutions/c/function/input.c +++ b/challenges/is-palindrome/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/is-palindrome/solutions/c/function/input.h b/challenges/is-palindrome/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/is-palindrome/solutions/c/function/input.h +++ b/challenges/is-palindrome/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/is-palindrome/solutions/c/function/solution.c b/challenges/is-palindrome/solutions/c/function/solution.c index cb538c5..20854f2 100644 --- a/challenges/is-palindrome/solutions/c/function/solution.c +++ b/challenges/is-palindrome/solutions/c/function/solution.c @@ -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"); diff --git a/challenges/is-palindrome/solutions/c/function/string.c b/challenges/is-palindrome/solutions/c/function/string.c index 75cef6b..54bbd53 100644 --- a/challenges/is-palindrome/solutions/c/function/string.c +++ b/challenges/is-palindrome/solutions/c/function/string.c @@ -1,46 +1,67 @@ #include "string.h" -#include -#include -#include +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; -} diff --git a/challenges/is-palindrome/solutions/c/function/string.h b/challenges/is-palindrome/solutions/c/function/string.h index bab4d82..531e2d8 100644 --- a/challenges/is-palindrome/solutions/c/function/string.h +++ b/challenges/is-palindrome/solutions/c/function/string.h @@ -1,23 +1,57 @@ #ifndef __STRING__ #define __STRING__ +#include #include +#include +#include +#include + +#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 diff --git a/challenges/is-prime-number/solutions/c/function/solution.c b/challenges/is-prime-number/solutions/c/function/solution.c index 98695d4..138ccad 100644 --- a/challenges/is-prime-number/solutions/c/function/solution.c +++ b/challenges/is-prime-number/solutions/c/function/solution.c @@ -2,8 +2,8 @@ #include #include -bool is_prime_number(int number) { - for (int iteration = 2; iteration < number; iteration++) { +bool is_prime_number(unsigned long number) { + for (unsigned long iteration = 2; iteration < number; iteration++) { if (number % iteration == 0) { return false; } @@ -12,8 +12,8 @@ bool is_prime_number(int number) { } int main() { - int number; - scanf("%d", &number); + unsigned long number; + scanf("%lu", &number); bool is_prime = is_prime_number(number); printf("%s\n", is_prime ? "true" : "false"); return EXIT_SUCCESS; diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/character.c b/challenges/look-and-say-sequence-conway/solutions/c/function/character.c index d0fdd2f..bd60265 100644 --- a/challenges/look-and-say-sequence-conway/solutions/c/function/character.c +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/character.h b/challenges/look-and-say-sequence-conway/solutions/c/function/character.h index 70bf124..1535477 100644 --- a/challenges/look-and-say-sequence-conway/solutions/c/function/character.h +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/input.c b/challenges/look-and-say-sequence-conway/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/look-and-say-sequence-conway/solutions/c/function/input.c +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/input.h b/challenges/look-and-say-sequence-conway/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/look-and-say-sequence-conway/solutions/c/function/input.h +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c b/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c index 80c6402..33bfb8a 100644 --- a/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c @@ -24,5 +24,7 @@ int main() { index += number_of_appearances - 1; } printf("%s\n", result); + free(string); + free(result); return EXIT_SUCCESS; } diff --git a/challenges/prefix-suffix/solutions/c/function/character.c b/challenges/prefix-suffix/solutions/c/function/character.c index 49d4f00..3e8c0ff 100644 --- a/challenges/prefix-suffix/solutions/c/function/character.c +++ b/challenges/prefix-suffix/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/prefix-suffix/solutions/c/function/character.h b/challenges/prefix-suffix/solutions/c/function/character.h index 0fab88f..e3a2037 100644 --- a/challenges/prefix-suffix/solutions/c/function/character.h +++ b/challenges/prefix-suffix/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/prefix-suffix/solutions/c/function/input.c b/challenges/prefix-suffix/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/prefix-suffix/solutions/c/function/input.c +++ b/challenges/prefix-suffix/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/prefix-suffix/solutions/c/function/input.h b/challenges/prefix-suffix/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/prefix-suffix/solutions/c/function/input.h +++ b/challenges/prefix-suffix/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/prefix-suffix/solutions/c/function/string.c b/challenges/prefix-suffix/solutions/c/function/string.c index 72dd225..42bf52c 100644 --- a/challenges/prefix-suffix/solutions/c/function/string.c +++ b/challenges/prefix-suffix/solutions/c/function/string.c @@ -1,11 +1,5 @@ #include "string.h" -#include -#include -#include - -#include "character.h" - bool string_starts_with(char* string, char* prefix) { size_t string_length = strlen(string); size_t prefix_length = strlen(prefix); diff --git a/challenges/prefix-suffix/solutions/c/function/string.h b/challenges/prefix-suffix/solutions/c/function/string.h index 4b4998a..f5917d4 100644 --- a/challenges/prefix-suffix/solutions/c/function/string.h +++ b/challenges/prefix-suffix/solutions/c/function/string.h @@ -2,6 +2,10 @@ #define __STRING__ #include +#include +#include + +#include "character.h" /** * @brief Returns true if the string is a prefix of another string. diff --git a/challenges/prime-numbers-decomposition/solutions/c/function/solution.c b/challenges/prime-numbers-decomposition/solutions/c/function/solution.c index f8cbea5..3500bc0 100644 --- a/challenges/prime-numbers-decomposition/solutions/c/function/solution.c +++ b/challenges/prime-numbers-decomposition/solutions/c/function/solution.c @@ -28,8 +28,10 @@ int* get_dividers_list(int number, int* dividers_length) { bool is_prime_number(int number) { int dividers_length; - get_dividers_list(number, ÷rs_length); - return dividers_length == 2; + int* dividers_list = get_dividers_list(number, ÷rs_length); + bool result = dividers_length == 2; + free(dividers_list); + return result; } int sum_multiply_numbers(int* numbers, int numbers_length) { diff --git a/challenges/print-pyramid/solutions/c/function/character.c b/challenges/print-pyramid/solutions/c/function/character.c index 1c3170d..af36467 100644 --- a/challenges/print-pyramid/solutions/c/function/character.c +++ b/challenges/print-pyramid/solutions/c/function/character.c @@ -1,9 +1,5 @@ #include "character.h" -#include -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/print-pyramid/solutions/c/function/character.h b/challenges/print-pyramid/solutions/c/function/character.h index 5341353..e2657c9 100644 --- a/challenges/print-pyramid/solutions/c/function/character.h +++ b/challenges/print-pyramid/solutions/c/function/character.h @@ -1,7 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include #include +#include /** * @brief Append a character to a string, assuming string points to an array diff --git a/challenges/print-pyramid/solutions/c/function/input.c b/challenges/print-pyramid/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/print-pyramid/solutions/c/function/input.c +++ b/challenges/print-pyramid/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/print-pyramid/solutions/c/function/input.h b/challenges/print-pyramid/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/print-pyramid/solutions/c/function/input.h +++ b/challenges/print-pyramid/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/print-pyramid/solutions/c/function/solution.c b/challenges/print-pyramid/solutions/c/function/solution.c index 7322a3b..210c522 100644 --- a/challenges/print-pyramid/solutions/c/function/solution.c +++ b/challenges/print-pyramid/solutions/c/function/solution.c @@ -7,10 +7,10 @@ int main() { char* type = input(); - int height; - scanf("%d", &height); + unsigned long height; + scanf("%lu", &height); - int step = strcmp(type, "normal") == 0 ? 1 : height; + unsigned long step = strcmp(type, "normal") == 0 ? 1 : height; while ((strcmp(type, "normal") == 0 && step <= height) || (strcmp(type, "reverse") == 0 && step != 0)) { size_t numberOfStars = (step * 2) - 1; size_t totalNumberOfLocations = (height * 2) - 1; @@ -23,5 +23,6 @@ int main() { step = strcmp(type, "normal") == 0 ? step + 1 : step - 1; } + free(type); return EXIT_SUCCESS; } diff --git a/challenges/reverse-polish-notation/solutions/c/function/character.c b/challenges/reverse-polish-notation/solutions/c/function/character.c index 49d4f00..3e8c0ff 100644 --- a/challenges/reverse-polish-notation/solutions/c/function/character.c +++ b/challenges/reverse-polish-notation/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/reverse-polish-notation/solutions/c/function/character.h b/challenges/reverse-polish-notation/solutions/c/function/character.h index 0fab88f..e3a2037 100644 --- a/challenges/reverse-polish-notation/solutions/c/function/character.h +++ b/challenges/reverse-polish-notation/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/reverse-polish-notation/solutions/c/function/input.c b/challenges/reverse-polish-notation/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/reverse-polish-notation/solutions/c/function/input.c +++ b/challenges/reverse-polish-notation/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/reverse-polish-notation/solutions/c/function/input.h b/challenges/reverse-polish-notation/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/reverse-polish-notation/solutions/c/function/input.h +++ b/challenges/reverse-polish-notation/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/reverse-polish-notation/solutions/c/function/solution.c b/challenges/reverse-polish-notation/solutions/c/function/solution.c index cd8a8eb..8f08f1c 100644 --- a/challenges/reverse-polish-notation/solutions/c/function/solution.c +++ b/challenges/reverse-polish-notation/solutions/c/function/solution.c @@ -20,7 +20,7 @@ bool is_integer(char* string) { int main() { char* string = input(); - struct Stack* stack = stack_initialization(); + struct stack* stack = stack_initialization(); char* token = strtok(string, " "); while (token != NULL) { if (is_integer(token)) { @@ -45,5 +45,7 @@ int main() { token = strtok(NULL, " "); } printf("%ld\n", (intptr_t)stack_pop(stack)); + free(string); + stack_free(stack); return EXIT_SUCCESS; } diff --git a/challenges/reverse-polish-notation/solutions/c/function/stack.c b/challenges/reverse-polish-notation/solutions/c/function/stack.c index f06d6c2..ecde934 100644 --- a/challenges/reverse-polish-notation/solutions/c/function/stack.c +++ b/challenges/reverse-polish-notation/solutions/c/function/stack.c @@ -1,11 +1,9 @@ #include "stack.h" -#include -#include - -struct Stack *stack_initialization() { - struct Stack *stack = malloc(sizeof(*stack)); +struct stack *stack_initialization() { + struct stack *stack = malloc(sizeof(struct stack)); if (stack == NULL) { + perror("Error (stack_initialization)"); exit(EXIT_FAILURE); } stack->first = NULL; @@ -13,9 +11,15 @@ struct Stack *stack_initialization() { return stack; } -void stack_push(struct Stack *stack, void *data) { - struct Node *node_new = malloc(sizeof(*node_new)); - if (stack == NULL || data == NULL) { +void stack_push(struct stack *stack, void *data) { + if (stack == NULL) { + errno = EINVAL; + perror("Error (stack_push)"); + exit(EXIT_FAILURE); + } + struct stack_node *node_new = malloc(sizeof(struct stack_node)); + if (data == NULL) { + perror("Error (stack_push)"); exit(EXIT_FAILURE); } node_new->data = data; @@ -24,11 +28,13 @@ void stack_push(struct Stack *stack, void *data) { stack->length = stack->length + 1; } -void *stack_pop(struct Stack *stack) { +void *stack_pop(struct stack *stack) { if (stack == NULL) { + errno = EINVAL; + perror("Error (stack_pop)"); exit(EXIT_FAILURE); } - struct Node *node = stack->first; + struct stack_node *node = stack->first; void *data = NULL; if (node != NULL) { stack->first = node->next; @@ -38,3 +44,18 @@ void *stack_pop(struct Stack *stack) { stack->length = stack->length - 1; return data; } + +void stack_free(struct stack *stack) { + if (stack == NULL) { + errno = EINVAL; + perror("Error (stack_free)"); + exit(EXIT_FAILURE); + } + struct stack_node *node = stack->first; + while (node != NULL) { + struct stack_node *node_next = node->next; + free(node); + node = node_next; + } + free(stack); +} diff --git a/challenges/reverse-polish-notation/solutions/c/function/stack.h b/challenges/reverse-polish-notation/solutions/c/function/stack.h index 068577a..9a39562 100644 --- a/challenges/reverse-polish-notation/solutions/c/function/stack.h +++ b/challenges/reverse-polish-notation/solutions/c/function/stack.h @@ -1,23 +1,54 @@ #ifndef __STACK__ #define __STACK__ +#include +#include #include -// LIFO = Last In First Out -struct Stack { - struct Node *first; +/** + * @brief Stack structure => LIFO (Last In First Out). + */ +struct stack { + struct stack_node *first; size_t length; }; -struct Node { +/** + * @brief Stack node structure. + */ +struct stack_node { void *data; - struct Node *next; + struct stack_node *next; }; -struct Stack *stack_initialization(); +/** + * @brief Stack initialization. + * + * @return struct stack* + */ +struct stack *stack_initialization(); -void stack_push(struct Stack *stack, void *data); +/** + * @brief Push data to stack. + * + * @param stack + * @param data + */ +void stack_push(struct stack *stack, void *data); -void *stack_pop(struct Stack *stack); +/** + * @brief Pop data from stack. + * + * @param stack + * @return void* + */ +void *stack_pop(struct stack *stack); + +/** + * @brief Frees the stack. + * + * @param stack + */ +void stack_free(struct stack *stack); #endif diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c index cbb2521..d34cab8 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c @@ -1,16 +1,10 @@ #include "array_2D_int.h" -#include -#include -#include - -#include "character.h" - void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_columns) { - for (size_t i = 0; i < number_of_rows; i++) { - for (size_t j = 0; j < number_of_columns; j++) { - printf("%d", array[i][j]); - if (j != number_of_columns - 1) { + for (size_t row = 0; row < number_of_rows; row++) { + for (size_t column = 0; column < number_of_columns; column++) { + printf("%d", array[row][column]); + if (column != number_of_columns - 1) { printf(" "); } } @@ -19,7 +13,7 @@ void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_col } int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) { - int **array = malloc(sizeof(int *)); + int **array = malloc(sizeof(int)); *number_of_rows = 1; *number_of_columns = 1; array[0] = malloc(*number_of_columns * sizeof(int)); @@ -60,13 +54,13 @@ int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) { } int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns) { - int **rotated_array = malloc(*number_of_columns * sizeof(int *)); - for (size_t i = 0; i < *number_of_columns; i++) { - rotated_array[i] = malloc(*number_of_rows * sizeof(int)); + int **rotated_array = malloc(*number_of_rows * sizeof(int)); + for (size_t row = 0; row < *number_of_rows; row++) { + rotated_array[row] = malloc(*number_of_columns * sizeof(int)); } - for (size_t i = 0; i < *number_of_columns; i++) { - for (size_t j = 0; j < *number_of_rows; j++) { - rotated_array[i][j] = array[*number_of_rows - i - 1][j]; + for (size_t row = 0; row < *number_of_columns; row++) { + for (size_t column = 0; column < *number_of_rows; column++) { + rotated_array[row][column] = array[*number_of_rows - row - 1][column]; } } return rotated_array; @@ -74,12 +68,12 @@ int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *num int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) { int **rotated_array = malloc(*number_of_columns * sizeof(int *)); - for (size_t i = 0; i < *number_of_columns; i++) { - rotated_array[i] = malloc(*number_of_rows * sizeof(int)); + for (size_t row = 0; row < *number_of_columns; row++) { + rotated_array[row] = malloc(*number_of_rows * sizeof(int)); } - for (size_t i = 0; i < *number_of_columns; i++) { - for (size_t j = 0; j < *number_of_rows; j++) { - rotated_array[i][j] = array[*number_of_rows - j - 1][i]; + for (size_t row = 0; row < *number_of_columns; row++) { + for (size_t column = 0; column < *number_of_rows; column++) { + rotated_array[row][column] = array[*number_of_rows - column - 1][row]; } } size_t number_of_rows_temp = *number_of_rows; @@ -89,9 +83,28 @@ int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_ro } int **array_2D_int_rotate_90_degrees_anticlockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) { - int **result = array_2D_int_rotate_90_degrees_clockwise(array, number_of_rows, number_of_columns); - result = array_2D_int_rotate_90_degrees_clockwise(result, number_of_rows, number_of_columns); - result = array_2D_int_rotate_90_degrees_clockwise(result, number_of_rows, number_of_columns); - result = array_2D_int_reverse_rows(result, number_of_rows, number_of_columns); - return result; + int **result_1 = array_2D_int_rotate_90_degrees_clockwise(array, number_of_rows, number_of_columns); + size_t number_of_rows_temp = *number_of_rows; + + int **result_2 = array_2D_int_rotate_90_degrees_clockwise(result_1, number_of_rows, number_of_columns); + for (size_t row = 0; row < number_of_rows_temp; row++) { + free(result_1[row]); + } + free(result_1); + number_of_rows_temp = *number_of_rows; + + int **result_3 = array_2D_int_rotate_90_degrees_clockwise(result_2, number_of_rows, number_of_columns); + for (size_t row = 0; row < number_of_rows_temp; row++) { + free(result_2[row]); + } + free(result_2); + number_of_rows_temp = *number_of_rows; + + int **result_4 = array_2D_int_reverse_rows(result_3, number_of_rows, number_of_columns); + for (size_t row = 0; row < number_of_rows_temp; row++) { + free(result_3[row]); + } + free(result_3); + + return result_4; } diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h index c42db5a..2b478c6 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h @@ -1,7 +1,11 @@ #ifndef __ARRAY_2D_INT__ #define __ARRAY_2D_INT__ +#include #include +#include + +#include "character.h" /** * @brief Prints a 2D array of integers. diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.c index 49d4f00..3e8c0ff 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.c +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.h b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.h index 0fab88f..e3a2037 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.h +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.c +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.h b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.h +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c index 214340f..db76433 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c @@ -8,19 +8,20 @@ int main() { size_t number_of_rows = 0; size_t number_of_columns = 0; char *direction = input(); - int **array = array_2D_int_input(&number_of_rows, &number_of_columns); + int **array_input = array_2D_int_input(&number_of_rows, &number_of_columns); + int **array; if (strcmp(direction, "clockwise") == 0) { - array = array_2D_int_rotate_90_degrees_clockwise(array, &number_of_rows, &number_of_columns); + array = array_2D_int_rotate_90_degrees_clockwise(array_input, &number_of_rows, &number_of_columns); } else { - array = array_2D_int_rotate_90_degrees_anticlockwise(array, &number_of_rows, &number_of_columns); + array = array_2D_int_rotate_90_degrees_anticlockwise(array_input, &number_of_rows, &number_of_columns); } array_2D_int_print(array, number_of_rows, number_of_columns); - for (size_t i = 0; i < number_of_rows; i++) { - free(array[i]); + free(direction); + for (size_t row = 0; row < number_of_rows; row++) { + free(array[row]); } free(array); - free(direction); return EXIT_SUCCESS; } diff --git a/challenges/slugify/solutions/c/function/character.c b/challenges/slugify/solutions/c/function/character.c index 1d8f580..33ab8c0 100644 --- a/challenges/slugify/solutions/c/function/character.c +++ b/challenges/slugify/solutions/c/function/character.c @@ -1,9 +1,5 @@ #include "character.h" -#include -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/challenges/slugify/solutions/c/function/character.h b/challenges/slugify/solutions/c/function/character.h index d28e833..ce6feb2 100644 --- a/challenges/slugify/solutions/c/function/character.h +++ b/challenges/slugify/solutions/c/function/character.h @@ -2,6 +2,8 @@ #define __CHARACTER__ #include +#include +#include /** * @brief Append a character to a string, assuming string points to an array diff --git a/challenges/slugify/solutions/c/function/input.c b/challenges/slugify/solutions/c/function/input.c index c07bb6d..0e4515c 100644 --- a/challenges/slugify/solutions/c/function/input.c +++ b/challenges/slugify/solutions/c/function/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/challenges/slugify/solutions/c/function/input.h b/challenges/slugify/solutions/c/function/input.h index d432398..180ae36 100644 --- a/challenges/slugify/solutions/c/function/input.h +++ b/challenges/slugify/solutions/c/function/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/challenges/slugify/solutions/c/function/solution.c b/challenges/slugify/solutions/c/function/solution.c index b9ff7f4..34d95b4 100644 --- a/challenges/slugify/solutions/c/function/solution.c +++ b/challenges/slugify/solutions/c/function/solution.c @@ -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; } diff --git a/challenges/slugify/solutions/c/function/string.c b/challenges/slugify/solutions/c/function/string.c index a5d7f7f..aa9e069 100644 --- a/challenges/slugify/solutions/c/function/string.c +++ b/challenges/slugify/solutions/c/function/string.c @@ -1,66 +1,67 @@ #include "string.h" -#include -#include -#include +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'; } } } diff --git a/challenges/slugify/solutions/c/function/string.h b/challenges/slugify/solutions/c/function/string.h index 9088029..478f6f8 100644 --- a/challenges/slugify/solutions/c/function/string.h +++ b/challenges/slugify/solutions/c/function/string.h @@ -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 +#include +#include + +#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. diff --git a/templates/solution/c/character.c b/templates/solution/c/character.c index 49d4f00..3e8c0ff 100644 --- a/templates/solution/c/character.c +++ b/templates/solution/c/character.c @@ -1,8 +1,5 @@ #include "character.h" -#include -#include - void character_append(char* string, char character) { size_t length = strlen(string); string[length] = character; diff --git a/templates/solution/c/character.h b/templates/solution/c/character.h index 0fab88f..e3a2037 100644 --- a/templates/solution/c/character.h +++ b/templates/solution/c/character.h @@ -1,6 +1,9 @@ #ifndef __CHARACTER__ #define __CHARACTER__ +#include +#include + /** * @brief Append a character to a string, assuming string points to an array * with enough space. diff --git a/templates/solution/c/input.c b/templates/solution/c/input.c index c07bb6d..0e4515c 100644 --- a/templates/solution/c/input.c +++ b/templates/solution/c/input.c @@ -1,10 +1,5 @@ #include "input.h" -#include -#include - -#include "character.h" - char* input() { char character; size_t length = 1; diff --git a/templates/solution/c/input.h b/templates/solution/c/input.h index d432398..180ae36 100644 --- a/templates/solution/c/input.h +++ b/templates/solution/c/input.h @@ -1,6 +1,11 @@ #ifndef __INPUT__ #define __INPUT__ +#include +#include + +#include "character.h" + /** * @brief Read a line from stdin. * diff --git a/templates/solution/c/solution.c b/templates/solution/c/solution.c index 30ce41e..7e0f37f 100644 --- a/templates/solution/c/solution.c +++ b/templates/solution/c/solution.c @@ -6,5 +6,6 @@ int main() { char *string = input(); printf("Hello, %s!\n", string); + free(string); return EXIT_SUCCESS; }