diff --git a/challenges/prefix-suffix/solutions/c/function/README.md b/challenges/prefix-suffix/solutions/c/function/README.md new file mode 100644 index 0000000..94bf3be --- /dev/null +++ b/challenges/prefix-suffix/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# prefix-suffix/c/function + +Created by [@Divlo](https://github.com/Divlo) on 2 December 2021. diff --git a/challenges/prefix-suffix/solutions/c/function/character.c b/challenges/prefix-suffix/solutions/c/function/character.c new file mode 100644 index 0000000..49d4f00 --- /dev/null +++ b/challenges/prefix-suffix/solutions/c/function/character.c @@ -0,0 +1,10 @@ +#include "character.h" + +#include +#include + +void character_append(char* string, char character) { + size_t length = strlen(string); + string[length] = character; + string[length + 1] = '\0'; +} diff --git a/challenges/prefix-suffix/solutions/c/function/character.h b/challenges/prefix-suffix/solutions/c/function/character.h new file mode 100644 index 0000000..0fab88f --- /dev/null +++ b/challenges/prefix-suffix/solutions/c/function/character.h @@ -0,0 +1,13 @@ +#ifndef __CHARACTER__ +#define __CHARACTER__ + +/** + * @brief Append a character to a string, assuming string points to an array + * with enough space. + * + * @param string + * @param character + */ +void character_append(char* string, char character); + +#endif diff --git a/challenges/prefix-suffix/solutions/c/function/input.c b/challenges/prefix-suffix/solutions/c/function/input.c new file mode 100644 index 0000000..c07bb6d --- /dev/null +++ b/challenges/prefix-suffix/solutions/c/function/input.c @@ -0,0 +1,19 @@ +#include "input.h" + +#include +#include + +#include "character.h" + +char* input() { + char character; + size_t length = 1; + char* string = malloc(length * sizeof(char)); + *string = '\0'; + while ((character = getchar()) != '\n' && character != EOF) { + length++; + string = realloc(string, length * sizeof(char)); + character_append(string, character); + } + return string; +} diff --git a/challenges/prefix-suffix/solutions/c/function/input.h b/challenges/prefix-suffix/solutions/c/function/input.h new file mode 100644 index 0000000..d432398 --- /dev/null +++ b/challenges/prefix-suffix/solutions/c/function/input.h @@ -0,0 +1,11 @@ +#ifndef __INPUT__ +#define __INPUT__ + +/** + * @brief Read a line from stdin. + * + * @return char* + */ +char* input(); + +#endif diff --git a/challenges/prefix-suffix/solutions/c/function/solution.c b/challenges/prefix-suffix/solutions/c/function/solution.c new file mode 100644 index 0000000..8a8a0bd --- /dev/null +++ b/challenges/prefix-suffix/solutions/c/function/solution.c @@ -0,0 +1,18 @@ +#include +#include +#include + +#include "input.h" +#include "string.h" + +int main() { + char* string = input(); + char* string2 = input(); + bool is_preffix = string_starts_with(string, string2); + bool is_suffix = string_ends_with(string, string2); + free(string); + free(string2); + printf("%s\n", is_preffix ? "true" : "false"); + printf("%s\n", is_suffix ? "true" : "false"); + return EXIT_SUCCESS; +} diff --git a/challenges/prefix-suffix/solutions/c/function/string.c b/challenges/prefix-suffix/solutions/c/function/string.c new file mode 100644 index 0000000..72dd225 --- /dev/null +++ b/challenges/prefix-suffix/solutions/c/function/string.c @@ -0,0 +1,38 @@ +#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); + bool is_prefix = string_length >= prefix_length; + for (size_t index = 0; index < prefix_length && index < string_length && is_prefix; index++) { + if (prefix[index] != string[index]) { + is_prefix = false; + } + } + return is_prefix; +} + +bool string_ends_with(char* string, char* suffix) { + size_t string_length = strlen(string); + size_t suffix_length = strlen(suffix); + int index_start_of_the_end = string_length - suffix_length; + bool is_suffix = true; + if (index_start_of_the_end < 0) { + is_suffix = false; + } else { + size_t suffix_index = 0; + for (size_t string_index = index_start_of_the_end; string_index < string_length && is_suffix; string_index++) { + if (string[string_index] != suffix[suffix_index]) { + is_suffix = false; + } + suffix_index++; + } + } + return is_suffix; +} diff --git a/challenges/prefix-suffix/solutions/c/function/string.h b/challenges/prefix-suffix/solutions/c/function/string.h new file mode 100644 index 0000000..4b4998a --- /dev/null +++ b/challenges/prefix-suffix/solutions/c/function/string.h @@ -0,0 +1,26 @@ +#ifndef __STRING__ +#define __STRING__ + +#include + +/** + * @brief Returns true if the string is a prefix of another string. + * + * @param string + * @param prefix + * @return true + * @return false + */ +bool string_starts_with(char* string, char* prefix); + +/** + * @brief Returns true if the string is a suffix of another string. + * + * @param string + * @param suffix + * @return true + * @return false + */ +bool string_ends_with(char* string, char* suffix); + +#endif