From 0c078abb893f112b32301f6ee96c3baf5a578b49 Mon Sep 17 00:00:00 2001 From: Divlo Date: Wed, 29 Sep 2021 22:20:41 +0200 Subject: [PATCH] feat(solutions): add `camel-case/c/function` --- .../camel-case/solutions/c/function/README.md | 3 + .../solutions/c/function/character.c | 20 ++++++ .../solutions/c/function/character.h | 21 ++++++ .../camel-case/solutions/c/function/input.c | 19 ++++++ .../camel-case/solutions/c/function/input.h | 11 ++++ .../solutions/c/function/solution.c | 15 +++++ .../camel-case/solutions/c/function/string.c | 66 +++++++++++++++++++ .../camel-case/solutions/c/function/string.h | 44 +++++++++++++ .../solutions/c/function/character.c | 9 +-- .../solutions/c/function/input.c | 2 + 10 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 challenges/camel-case/solutions/c/function/README.md create mode 100644 challenges/camel-case/solutions/c/function/character.c create mode 100644 challenges/camel-case/solutions/c/function/character.h create mode 100644 challenges/camel-case/solutions/c/function/input.c create mode 100644 challenges/camel-case/solutions/c/function/input.h create mode 100644 challenges/camel-case/solutions/c/function/solution.c create mode 100644 challenges/camel-case/solutions/c/function/string.c create mode 100644 challenges/camel-case/solutions/c/function/string.h diff --git a/challenges/camel-case/solutions/c/function/README.md b/challenges/camel-case/solutions/c/function/README.md new file mode 100644 index 0000000..4d624ac --- /dev/null +++ b/challenges/camel-case/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# camel-case/c/function + +Created by [@Divlo](https://github.com/Divlo) on 29 September 2021. diff --git a/challenges/camel-case/solutions/c/function/character.c b/challenges/camel-case/solutions/c/function/character.c new file mode 100644 index 0000000..b285fc7 --- /dev/null +++ b/challenges/camel-case/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/camel-case/solutions/c/function/character.h b/challenges/camel-case/solutions/c/function/character.h new file mode 100644 index 0000000..62699a7 --- /dev/null +++ b/challenges/camel-case/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/camel-case/solutions/c/function/input.c b/challenges/camel-case/solutions/c/function/input.c new file mode 100644 index 0000000..c07bb6d --- /dev/null +++ b/challenges/camel-case/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/camel-case/solutions/c/function/input.h b/challenges/camel-case/solutions/c/function/input.h new file mode 100644 index 0000000..9cd0a22 --- /dev/null +++ b/challenges/camel-case/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/camel-case/solutions/c/function/solution.c b/challenges/camel-case/solutions/c/function/solution.c new file mode 100644 index 0000000..be9aab3 --- /dev/null +++ b/challenges/camel-case/solutions/c/function/solution.c @@ -0,0 +1,15 @@ +#include +#include + +#include "character.h" +#include "input.h" +#include "string.h" + +int main() { + char* string = input(); + string = string_trim(string); + string = string_camelCase(string); + printf("%s\n", string); + free(string); + return EXIT_SUCCESS; +} diff --git a/challenges/camel-case/solutions/c/function/string.c b/challenges/camel-case/solutions/c/function/string.c new file mode 100644 index 0000000..58cb61e --- /dev/null +++ b/challenges/camel-case/solutions/c/function/string.c @@ -0,0 +1,66 @@ +#include "string.h" + +#include +#include +#include + +#include "character.h" + +char* string_trim_start(char* string) { + size_t string_length = strlen(string); + char* result = malloc(sizeof(char) * (string_length + 1)); + size_t index_space = 0; + while (string[index_space] == ' ') { + index_space++; + } + for (size_t index = index_space; index < string_length; index++) { + character_append(result, string[index]); + } + return result; +} + +char* string_trim_end(char* string) { + size_t string_length = strlen(string); + char* result = malloc(sizeof(char) * (string_length + 1)); + size_t index_space = string_length - 1; + while (string[index_space] == ' ') { + index_space--; + } + for (size_t index = 0; index < index_space + 1; index++) { + character_append(result, string[index]); + } + return result; +} + +char* string_trim(char* string) { + char* result = string_trim_start(string); + result = string_trim_end(result); + return result; +} + +char* string_capitalize(char* string) { + size_t string_length = strlen(string); + if (string_length > 0) { + string[0] = character_to_upper(string[0]); + } + return string; +} + +char* string_camelCase(char* string) { + size_t string_length = strlen(string); + char* result = malloc(sizeof(char) * (string_length + 1)); + int words = 0; + char* current = malloc(sizeof(char) * (string_length + 1)); + for (size_t index = 0; index < string_length; index++) { + if (string[index] == ' ') { + strcat(result, words == 0 ? current : string_capitalize(current)); + memset(current, 0, sizeof(char) * (string_length + 1)); + words++; + } else { + character_append(current, string[index]); + } + } + strcat(result, words == 0 ? current : string_capitalize(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 new file mode 100644 index 0000000..4680c5c --- /dev/null +++ b/challenges/camel-case/solutions/c/function/string.h @@ -0,0 +1,44 @@ +#ifndef STRING_H +#define STRING_H + +/** + * @brief Removes all whitespace from the start of a string. + * + * @param string + * @return char* + */ +char* string_trim_start(char* string); + +/** + * @brief Removes all whitespace from the end of a string. + * + * @param string + * @return char* + */ +char* string_trim_end(char* string); + +/** + * @brief Removes all whitespace from the start and end of a string. + * + * @param string + * @return char* + */ +char* string_trim(char* string); + +/** + * @brief Converts a string to camel case. + * + * @param string + * @return char* + */ +char* string_camelCase(char* string); + +/** + * @brief Capitalizes the string. + * + * @param string + * @return char* + */ +char* string_capitalize(char* string); + +#endif diff --git a/challenges/is-palindrome/solutions/c/function/character.c b/challenges/is-palindrome/solutions/c/function/character.c index d993469..b285fc7 100644 --- a/challenges/is-palindrome/solutions/c/function/character.c +++ b/challenges/is-palindrome/solutions/c/function/character.c @@ -10,10 +10,11 @@ void character_append(char* string, char character) { } const char character_to_upper(const char character) { - int a_ascii_code = (int)'a'; - int z_ascii_code = (int)'z'; - if (character >= a_ascii_code && character <= z_ascii_code) { - return character - 32; + 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/is-palindrome/solutions/c/function/input.c b/challenges/is-palindrome/solutions/c/function/input.c index cff07a3..dd5a28c 100644 --- a/challenges/is-palindrome/solutions/c/function/input.c +++ b/challenges/is-palindrome/solutions/c/function/input.c @@ -2,6 +2,8 @@ #include +#include "character.h" + char* input() { char character; size_t length = 1;