aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-10 17:12:12 +0200
committerAbigail <abigail@abigail.be>2021-05-10 17:12:12 +0200
commit7b5597d51092301df5e79f1104bb1403e42abd65 (patch)
tree80e27ad94038d41f543250dbecbc4e7786cb6b11
parent09b7571ec15816cfedda4642d1c3fc8e3e9b0f74 (diff)
downloadperlweeklychallenge-club-7b5597d51092301df5e79f1104bb1403e42abd65.tar.gz
perlweeklychallenge-club-7b5597d51092301df5e79f1104bb1403e42abd65.tar.bz2
perlweeklychallenge-club-7b5597d51092301df5e79f1104bb1403e42abd65.zip
C solutions for week 112
-rw-r--r--challenge-112/abigail/README.md2
-rw-r--r--challenge-112/abigail/c/ch-1.c78
-rw-r--r--challenge-112/abigail/c/ch-2.c25
3 files changed, 105 insertions, 0 deletions
diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md
index 574d51411f..c619dd35d2 100644
--- a/challenge-112/abigail/README.md
+++ b/challenge-112/abigail/README.md
@@ -35,6 +35,7 @@ Output: "/a"
### Solutions
* [AWK](awk/ch-1.awk)
* [Bash](bash/ch-1.sh)
+* [C](c/ch-1.c)
* [Perl](perl/ch-1.pl)
### Blog
@@ -53,6 +54,7 @@ This is just finding the `$n + 1` Fibonacci number.
### Solutions
* [AWK](awk/ch-2.awk)
* [Bash](bash/ch-2.sh)
+* [C](c/ch-2.c)
* [Perl](perl/ch-2.pl)
### Blog
diff --git a/challenge-112/abigail/c/ch-1.c b/challenge-112/abigail/c/ch-1.c
new file mode 100644
index 0000000000..d7445188a6
--- /dev/null
+++ b/challenge-112/abigail/c/ch-1.c
@@ -0,0 +1,78 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <stdbool.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file
+ */
+
+/*
+ * Here, we will "eliminate" parts (".", or ".." and its parent)
+ * by replacing the parts with slashes. Then we print the string,
+ * without printing 2 slashes in succession.
+ */
+
+int main (void) {
+ char * line = NULL;
+ size_t len = 0;
+ size_t str_len;
+
+ while ((str_len = getline (&line, &len, stdin)) != -1) {
+ line [str_len - 1] = '/';
+ for (size_t i = 0; i < str_len; i ++) {
+ if (line [i] == '.' && line [i - 1] == '/') {
+ /* Component starts with a . */
+ if (line [i + 1] == '/') {
+ line [i] = '/'; /* Current directory */
+ continue;
+ }
+ else {
+ if (line [i + 1] == '.' && line [i + 2] == '/') {
+ /* Parent directory. */
+ /* First wipe this component */
+ line [i] = '/';
+ line [i + 1] = '/';
+ /* Then wipe the previous component, if any. */
+ /* First, skip the slashes */
+ size_t j = i - 1;
+ while (j && line [j] == '/') {
+ j --;
+ }
+ /* Now, erase exactly one component */
+ while (j && line [j] != '/') {
+ line [j] = '/';
+ j --;
+ }
+ }
+ }
+ }
+ }
+ /* Get rid of trailing slashes */
+ while (str_len > 1 && line [str_len - 1] == '/') {
+ str_len --;
+ }
+ /* Print string, eliminating double slashes */
+ bool slash = false;
+ for (size_t i = 0; i < str_len; i ++) {
+ if (line [i] == '/') {
+ if (slash) {
+ continue;
+ }
+ slash = true;
+ }
+ else {
+ slash = false;
+ }
+ printf ("%c", line [i]);
+ }
+ printf ("\n");
+ }
+ free (line);
+
+ return (0);
+}
diff --git a/challenge-112/abigail/c/ch-2.c b/challenge-112/abigail/c/ch-2.c
new file mode 100644
index 0000000000..97c36677ab
--- /dev/null
+++ b/challenge-112/abigail/c/ch-2.c
@@ -0,0 +1,25 @@
+# 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
+ */
+
+int main (void) {
+ int n, f1, f2;
+
+ while (scanf ("%d", &n) == 1) {
+ for (f1 = 0, f2 = 1;n --;) {
+ f2 += f1;
+ f1 = f2 - f1;
+ }
+ printf ("%d\n", f2);
+ }
+
+ return (0);
+}