From 7d140cb8a96fbfc3971c6a87697b8114415dac9f Mon Sep 17 00:00:00 2001 From: Divlo Date: Wed, 29 Sep 2021 22:34:22 +0200 Subject: [PATCH] feat(solutions): add `acronyms/c/function` --- .../acronyms/solutions/c/function/README.md | 3 ++ .../acronyms/solutions/c/function/character.c | 20 ++++++++ .../acronyms/solutions/c/function/character.h | 21 +++++++++ .../acronyms/solutions/c/function/input.c | 19 ++++++++ .../acronyms/solutions/c/function/input.h | 11 +++++ .../acronyms/solutions/c/function/solution.c | 16 +++++++ .../acronyms/solutions/c/function/string.c | 47 +++++++++++++++++++ .../acronyms/solutions/c/function/string.h | 31 ++++++++++++ 8 files changed, 168 insertions(+) create mode 100644 challenges/acronyms/solutions/c/function/README.md create mode 100644 challenges/acronyms/solutions/c/function/character.c create mode 100644 challenges/acronyms/solutions/c/function/character.h create mode 100644 challenges/acronyms/solutions/c/function/input.c create mode 100644 challenges/acronyms/solutions/c/function/input.h create mode 100644 challenges/acronyms/solutions/c/function/solution.c create mode 100644 challenges/acronyms/solutions/c/function/string.c create mode 100644 challenges/acronyms/solutions/c/function/string.h diff --git a/challenges/acronyms/solutions/c/function/README.md b/challenges/acronyms/solutions/c/function/README.md new file mode 100644 index 0000000..bf88e47 --- /dev/null +++ b/challenges/acronyms/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# acronyms/c/function + +Created by [@Divlo](https://github.com/Divlo) on 29 September 2021. diff --git a/challenges/acronyms/solutions/c/function/character.c b/challenges/acronyms/solutions/c/function/character.c new file mode 100644 index 0000000..b285fc7 --- /dev/null +++ b/challenges/acronyms/solutions/c/function/character.c @@ -0,0 +1,20 @@ +#include "character.h" + +#include +#include + +void character_append(char* string, char character) { + size_t length = strlen(string); + string[length] = character; + string[length + 1] = '\0'; +} + +const char character_to_upper(const 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); + } + return character; +} diff --git a/challenges/acronyms/solutions/c/function/character.h b/challenges/acronyms/solutions/c/function/character.h new file mode 100644 index 0000000..62699a7 --- /dev/null +++ b/challenges/acronyms/solutions/c/function/character.h @@ -0,0 +1,21 @@ +#ifndef CHARACTER_H +#define CHARACTER_H + +/** + * @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); + +/** + * @brief Converts the character to uppercase. + * + * @param character + * @return const char + */ +const char character_to_upper(const char character); + +#endif diff --git a/challenges/acronyms/solutions/c/function/input.c b/challenges/acronyms/solutions/c/function/input.c new file mode 100644 index 0000000..c07bb6d --- /dev/null +++ b/challenges/acronyms/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/acronyms/solutions/c/function/input.h b/challenges/acronyms/solutions/c/function/input.h new file mode 100644 index 0000000..9cd0a22 --- /dev/null +++ b/challenges/acronyms/solutions/c/function/input.h @@ -0,0 +1,11 @@ +#ifndef INPUT_H +#define INPUT_H + +/** + * @brief Read a line from stdin. + * + * @return char* + */ +char* input(); + +#endif diff --git a/challenges/acronyms/solutions/c/function/solution.c b/challenges/acronyms/solutions/c/function/solution.c new file mode 100644 index 0000000..4ac7b60 --- /dev/null +++ b/challenges/acronyms/solutions/c/function/solution.c @@ -0,0 +1,16 @@ +#include +#include + +#include "character.h" +#include "input.h" +#include "string.h" + +int main() { + char* string = input(); + string = string_replace(string, '"', '\0'); + string = string_to_upper(string); + string = string_acronym(string); + printf("%s\n", string); + free(string); + return EXIT_SUCCESS; +} diff --git a/challenges/acronyms/solutions/c/function/string.c b/challenges/acronyms/solutions/c/function/string.c new file mode 100644 index 0000000..e77fd2d --- /dev/null +++ b/challenges/acronyms/solutions/c/function/string.c @@ -0,0 +1,47 @@ +#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])); + } + return result; +} + +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; +} + +char* string_acronym(char* string) { + size_t string_length = strlen(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]); + } + } + character_append(result, current[0]); + free(current); + return result; +} diff --git a/challenges/acronyms/solutions/c/function/string.h b/challenges/acronyms/solutions/c/function/string.h new file mode 100644 index 0000000..8d9dcc1 --- /dev/null +++ b/challenges/acronyms/solutions/c/function/string.h @@ -0,0 +1,31 @@ +#ifndef STRING_H +#define STRING_H + +/** + * @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. + * + * @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); + +/** + * @brief Converts a string to its acronym. + * + * @param string + * @return char* + */ +char* string_acronym(char* string); + +#endif