From 6e4c96b475edc880be09ef9b41addf2737da2446 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 11 Jan 2021 18:59:03 +0100 Subject: C solution for week 95/part 1 --- challenge-095/abigail/README.md | 1 + challenge-095/abigail/c/ch-1.c | 47 +++++++++++++++++++++++++++++++++++++++ challenge-095/abigail/t/ctest.ini | 6 +++++ 3 files changed, 54 insertions(+) create mode 100644 challenge-095/abigail/c/ch-1.c diff --git a/challenge-095/abigail/README.md b/challenge-095/abigail/README.md index c0798385a0..752fd8111b 100644 --- a/challenge-095/abigail/README.md +++ b/challenge-095/abigail/README.md @@ -20,6 +20,7 @@ Output: 0 ~~~~ ### Solutions +* [C](c/ch-1.c) * [Node](node/ch-1.js) * [Perl](perl/ch-1.pl) diff --git a/challenge-095/abigail/c/ch-1.c b/challenge-095/abigail/c/ch-1.c new file mode 100644 index 0000000000..9ef4ace799 --- /dev/null +++ b/challenge-095/abigail/c/ch-1.c @@ -0,0 +1,47 @@ +# include +# include +# include +# include + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len = 0; + + /* + * Check whether a given number is a palindrome. We're only + * handling numbers consisting of ASCII digits. + */ + while ((str_len = getline (&line, &len, stdin)) != -1) { + str_len --; /* New line */ + bool is_palindrome = !!str_len; + + /* + * Check wether the characters are digits, and the same + * when counting from the end. Of strings of uneven length, + * don't check the middle character just yet. + */ + for (size_t i = 0; i < str_len / 2 && is_palindrome; i ++) { + if (line [i] < '0' || line [i] > '9' || + line [i] != line [str_len - i - 1]) { + is_palindrome = false; + } + } + /* + * If we have an odd number of characters, check whether the middle + * character is a digit, or a dot -- but if it's a dot, the string + * should be at least two characters in length. + */ + if (is_palindrome && str_len % 2) { + size_t mid = str_len / 2; + if ((line [mid] < '0' || line [mid] > '9') && + (line [mid] != '.' || str_len == 1)) { + is_palindrome = false; + } + } + printf ("%d\n", is_palindrome ? 1 : 0); + } + free (line); + + return (0); +} diff --git a/challenge-095/abigail/t/ctest.ini b/challenge-095/abigail/t/ctest.ini index 6e05838ac8..d725563f31 100644 --- a/challenge-095/abigail/t/ctest.ini +++ b/challenge-095/abigail/t/ctest.ini @@ -9,3 +9,9 @@ [1-5/javascript] skip = No script runs assertions available + +[1-4/c] +skip = No Unicode support + +[1-5/c] +skip = No Unicode support -- cgit