diff --git a/challenges/consecutive-numbers/solutions/c/function/README.md b/challenges/consecutive-numbers/solutions/c/function/README.md new file mode 100644 index 0000000..e6053ac --- /dev/null +++ b/challenges/consecutive-numbers/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# consecutive-numbers/c/function + +Created by [@Divlo](https://github.com/Divlo) on 12 October 2021. diff --git a/challenges/consecutive-numbers/solutions/c/function/character.c b/challenges/consecutive-numbers/solutions/c/function/character.c new file mode 100644 index 0000000..49d4f00 --- /dev/null +++ b/challenges/consecutive-numbers/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/consecutive-numbers/solutions/c/function/character.h b/challenges/consecutive-numbers/solutions/c/function/character.h new file mode 100644 index 0000000..0fab88f --- /dev/null +++ b/challenges/consecutive-numbers/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/consecutive-numbers/solutions/c/function/input.c b/challenges/consecutive-numbers/solutions/c/function/input.c new file mode 100644 index 0000000..c07bb6d --- /dev/null +++ b/challenges/consecutive-numbers/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/consecutive-numbers/solutions/c/function/input.h b/challenges/consecutive-numbers/solutions/c/function/input.h new file mode 100644 index 0000000..d432398 --- /dev/null +++ b/challenges/consecutive-numbers/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/consecutive-numbers/solutions/c/function/solution.c b/challenges/consecutive-numbers/solutions/c/function/solution.c new file mode 100644 index 0000000..3ffc84a --- /dev/null +++ b/challenges/consecutive-numbers/solutions/c/function/solution.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +#include "input.h" + +void print_couple(int* numbers, size_t couple_length) { + for (size_t index = 0; index < couple_length; index++) { + printf("%d", numbers[index]); + if (index != couple_length - 1) { + printf(" ; "); + } + } + printf("\n"); +} + +int main() { + size_t couple_length; + scanf("%ld\n", &couple_length); + char* string = input(); + char* token = strtok(string, " ; "); + size_t numbers_length = 1; + int* numbers = malloc(sizeof(int) * numbers_length); + while (token != NULL) { + if (strcmp(token, ";") != 0) { + int number; + sscanf(token, "%d", &number); + numbers[numbers_length - 1] = number; + numbers_length++; + numbers = realloc(numbers, sizeof(int) * numbers_length); + } + token = strtok(NULL, " "); + } + + for (size_t index = 0; index < numbers_length; index++) { + int* consecutive = malloc(sizeof(int) * couple_length); + consecutive[0] = numbers[index]; + size_t consecutive_length = 1; + for (size_t couple_index = 1; couple_index < couple_length; couple_index++) { + bool is_last_number = index + couple_index == numbers_length; + if (is_last_number) { + break; + } + if (numbers[index] + couple_index == numbers[index + couple_index]) { + consecutive[consecutive_length] = numbers[index] + couple_index; + consecutive_length++; + } + bool is_consecutive = consecutive_length == couple_length; + if (is_consecutive) { + print_couple(consecutive, consecutive_length); + } + } + free(consecutive); + } + free(numbers); + return EXIT_SUCCESS; +}