aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-11 18:59:03 +0100
committerAbigail <abigail@abigail.be>2021-01-11 19:02:57 +0100
commit6e4c96b475edc880be09ef9b41addf2737da2446 (patch)
tree2ce42d2aab43a11005dd9ee64c1e2a7a526f0176
parent1924bacb816d612d5e89096bde817963d9736e8a (diff)
downloadperlweeklychallenge-club-6e4c96b475edc880be09ef9b41addf2737da2446.tar.gz
perlweeklychallenge-club-6e4c96b475edc880be09ef9b41addf2737da2446.tar.bz2
perlweeklychallenge-club-6e4c96b475edc880be09ef9b41addf2737da2446.zip
C solution for week 95/part 1
-rw-r--r--challenge-095/abigail/README.md1
-rw-r--r--challenge-095/abigail/c/ch-1.c47
-rw-r--r--challenge-095/abigail/t/ctest.ini6
3 files changed, 54 insertions, 0 deletions
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 <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <stdbool.h>
+
+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