From 773f381f9f101873062d689bbd95651bb39e1802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Tue, 13 May 2025 23:11:55 +0200 Subject: [PATCH] feat(solutions): add a-phone-code/c/function --- .../solutions/c/function/README.md | 3 ++ .../solutions/c/function/character.c | 7 ++++ .../solutions/c/function/character.h | 16 ++++++++ .../a-phone-code/solutions/c/function/input.c | 14 +++++++ .../a-phone-code/solutions/c/function/input.h | 16 ++++++++ .../solutions/c/function/solution.c | 40 +++++++++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 challenges/a-phone-code/solutions/c/function/README.md create mode 100644 challenges/a-phone-code/solutions/c/function/character.c create mode 100644 challenges/a-phone-code/solutions/c/function/character.h create mode 100644 challenges/a-phone-code/solutions/c/function/input.c create mode 100644 challenges/a-phone-code/solutions/c/function/input.h create mode 100644 challenges/a-phone-code/solutions/c/function/solution.c diff --git a/challenges/a-phone-code/solutions/c/function/README.md b/challenges/a-phone-code/solutions/c/function/README.md new file mode 100644 index 0000000..bc918b7 --- /dev/null +++ b/challenges/a-phone-code/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# a-phone-code/c/function + +Created by [@theoludwig](https://github.com/theoludwig) on 13 May 2025. diff --git a/challenges/a-phone-code/solutions/c/function/character.c b/challenges/a-phone-code/solutions/c/function/character.c new file mode 100644 index 0000000..3e8c0ff --- /dev/null +++ b/challenges/a-phone-code/solutions/c/function/character.c @@ -0,0 +1,7 @@ +#include "character.h" + +void character_append(char* string, char character) { + size_t length = strlen(string); + string[length] = character; + string[length + 1] = '\0'; +} diff --git a/challenges/a-phone-code/solutions/c/function/character.h b/challenges/a-phone-code/solutions/c/function/character.h new file mode 100644 index 0000000..e3a2037 --- /dev/null +++ b/challenges/a-phone-code/solutions/c/function/character.h @@ -0,0 +1,16 @@ +#ifndef __CHARACTER__ +#define __CHARACTER__ + +#include +#include + +/** + * @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/a-phone-code/solutions/c/function/input.c b/challenges/a-phone-code/solutions/c/function/input.c new file mode 100644 index 0000000..0e4515c --- /dev/null +++ b/challenges/a-phone-code/solutions/c/function/input.c @@ -0,0 +1,14 @@ +#include "input.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/a-phone-code/solutions/c/function/input.h b/challenges/a-phone-code/solutions/c/function/input.h new file mode 100644 index 0000000..180ae36 --- /dev/null +++ b/challenges/a-phone-code/solutions/c/function/input.h @@ -0,0 +1,16 @@ +#ifndef __INPUT__ +#define __INPUT__ + +#include +#include + +#include "character.h" + +/** + * @brief Read a line from stdin. + * + * @return char* + */ +char* input(); + +#endif diff --git a/challenges/a-phone-code/solutions/c/function/solution.c b/challenges/a-phone-code/solutions/c/function/solution.c new file mode 100644 index 0000000..b9fd6b1 --- /dev/null +++ b/challenges/a-phone-code/solutions/c/function/solution.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +#include "input.h" + +int main() { + size_t count = (size_t)atoi(input()); + size_t prefix_length = 0; + + char* last = NULL; + for (size_t i = 1; i < count; i++) { + if (last == NULL) { + last = input(); + } + char* current = input(); + + size_t prefix_current_length = 0; + for (size_t j = 0; j < strlen(current); j++) { + if (current[j] == last[j]) { + prefix_current_length += 1; + } else { + break; + } + } + + if (prefix_length > prefix_current_length) { + prefix_length = prefix_current_length; + } else if (prefix_length == 0) { + prefix_length = prefix_current_length; + } + + last = current; + } + + printf("%ld\n", prefix_length); + return EXIT_SUCCESS; +}