aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-10-18 19:45:00 +0200
committerAbigail <abigail@abigail.be>2021-10-18 19:45:00 +0200
commit69f1d3147c97538c8b7e385e3973b408fcf352f3 (patch)
treea97c656b943b7065002bbf52fd6d44558e9805bf
parent9ff133bea3a7f0c19cb1e9b0171533d4cb3b7633 (diff)
downloadperlweeklychallenge-club-69f1d3147c97538c8b7e385e3973b408fcf352f3.tar.gz
perlweeklychallenge-club-69f1d3147c97538c8b7e385e3973b408fcf352f3.tar.bz2
perlweeklychallenge-club-69f1d3147c97538c8b7e385e3973b408fcf352f3.zip
C solutions for week 135
-rw-r--r--challenge-135/abigail/README.md2
-rw-r--r--challenge-135/abigail/c/ch-1.c72
-rw-r--r--challenge-135/abigail/c/ch-2.c59
3 files changed, 133 insertions, 0 deletions
diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md
index aa835b7f1e..6c58ec0530 100644
--- a/challenge-135/abigail/README.md
+++ b/challenge-135/abigail/README.md
@@ -2,8 +2,10 @@
## Part 1
+* [C](c/ch-1.c)
* [Perl](perl/ch-1.pl)
## Part 2
+* [C](c/ch-2.c)
* [Perl](perl/ch-2.pl)
diff --git a/challenge-135/abigail/c/ch-1.c b/challenge-135/abigail/c/ch-1.c
new file mode 100644
index 0000000000..13776746e5
--- /dev/null
+++ b/challenge-135/abigail/c/ch-1.c
@@ -0,0 +1,72 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <ctype.h>
+# include <stdbool.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file
+ */
+
+int main (void) {
+ char * line = NULL;
+ size_t len = 0;
+ size_t str_len;
+
+ while ((str_len = getline (&line, &len, stdin)) != -1) {
+ char * line_ptr = line;
+ bool done = false;
+ if (* line_ptr == '-' || * line_ptr == '+') {
+ str_len --;
+ line_ptr ++;
+ while (isspace (* line_ptr)) {
+ line_ptr ++;
+ str_len --;
+ }
+ }
+ /*
+ * Is it an integer?
+ */
+ char * line_ptr2 = line_ptr;
+ while (* line_ptr2 && * line_ptr2 != '\n') {
+ if (!isdigit (* line_ptr2)) {
+ printf ("not an integer\n");
+ done = true;
+ break;
+ }
+ line_ptr2 ++;
+ }
+ if (done) {
+ continue;
+ }
+
+ /*
+ * Is the length odd? (Note the string itself still contains a newline)
+ */
+ if (str_len % 2 == 1) {
+ printf ("even number of digits\n");
+ continue;
+ }
+
+ /*
+ * Long enough?
+ */
+ if (str_len < 4) {
+ printf ("too short\n");
+ continue;
+ }
+
+ size_t mid = (str_len - 3) / 2;
+ for (size_t i = 0; i < 3; i ++) {
+ printf ("%c", line_ptr [mid + i]);
+ }
+ printf ("\n");
+ }
+ free (line);
+
+ return (0);
+}
diff --git a/challenge-135/abigail/c/ch-2.c b/challenge-135/abigail/c/ch-2.c
new file mode 100644
index 0000000000..ba49db177e
--- /dev/null
+++ b/challenge-135/abigail/c/ch-2.c
@@ -0,0 +1,59 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
+ */
+
+short w [] = {1, 3, 1, 7, 3, 9, 1};
+
+int main (void) {
+ char * line = NULL;
+ size_t len = 0;
+ size_t str_len;
+
+ while ((str_len = getline (&line, &len, stdin)) != -1) {
+ char * line_ptr = line;
+ short valid = 1;
+ if (str_len == 8) {
+ int check = 0;
+ for (size_t i = 0; i < 7 && valid; i ++) {
+ char first;
+ int base;
+ if ('0' <= line_ptr [i] && line_ptr [i] <= '9') {
+ first = '0';
+ base = 0;
+ }
+ else {
+ if ('B' <= line_ptr [i] && line_ptr [i] <= 'Z' &&
+ line_ptr [i] != 'E' &&
+ line_ptr [i] != 'I' &&
+ line_ptr [i] != 'O' &&
+ line_ptr [i] != 'U') {
+ first = 'A';
+ base = 10;
+ }
+ else {
+ valid = 0;
+ }
+ }
+ check += (line_ptr [i] - first + base) * w [i];
+ }
+ if (check % 10 != 0) {
+ valid = 0;
+ }
+ }
+ else {
+ valid = 0;
+ }
+ printf ("%d\n", valid);
+ }
+ free (line);
+
+ return (0);
+}