about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoraabacchus <ben@bvnf.space>2021-05-19 00:11:26 +0100
committeraabacchus <ben@bvnf.space>2021-05-19 00:11:26 +0100
commit3a14f3cf7a1bd094ae73a4d2ca151464a9eda224 (patch)
tree21ca13dd11b0ea7f79feb110b954050b1656bf7a
downloadkandr-3a14f3cf7a1bd094ae73a4d2ca151464a9eda224.tar.gz
might as well make commits out of these
-rw-r--r--.gitignore2
-rw-r--r--1.2.c23
-rw-r--r--1.5.c17
-rw-r--r--1.6.c20
-rw-r--r--cat.c10
-rw-r--r--max.c11
-rw-r--r--wc.c19
7 files changed, 102 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d7756c2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+a.out
+*.o
diff --git a/1.2.c b/1.2.c
new file mode 100644
index 0000000..9614161
--- /dev/null
+++ b/1.2.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define LOWER 0
+#define UPPER 300
+#define STEP  20
+
+int main() {
+    float fahr;
+    size_t fahrs;
+    void *fahrp;
+    fahrs = sizeof(float);
+    fahrp = malloc(fahrs);
+    fahr = 0;
+    printf("value:%f size:%d pointer: %p\n", fahr, fahrs, fahrp);
+
+    printf("%3s\t%6s\n", "F", "C");
+    for (fahr=UPPER; fahr >= LOWER; fahr -= STEP) {
+        printf("%3.0f\t%6.2f\n", fahr, 5. * (fahr - 32.) / 9.);
+    }
+    free(fahrp);
+    return 0;
+}
diff --git a/1.5.c b/1.5.c
new file mode 100644
index 0000000..3bb16ad
--- /dev/null
+++ b/1.5.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+int main() {
+	int c, nb;
+	char inWord = 0;
+	while ((c = getchar()) != EOF) {
+		if (c == ' ' || c == '\n' || c == '\t') {
+			inWord = 0;
+		}
+		else if (inWord == 0) {
+			putchar('\n');
+			inWord = 1;
+		}
+		putchar(c);
+	}
+	return 0;
+}
diff --git a/1.6.c b/1.6.c
new file mode 100644
index 0000000..4ed1550
--- /dev/null
+++ b/1.6.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+
+int main () {
+	int c, i, nwhite, nother;
+	int ndigit[10] = {0};
+	nwhite = nother = 0;
+
+	while ((c = getchar()) != EOF)
+		if (c >= '0' && c <= '9')
+			++ndigit[c-'0'];
+		else if (c == ' ' || c == '\n' || c == '\t')
+			++nwhite;
+		else
+			++nother;
+	printf("digits =");
+	for (i = 0; i < 10; ++i)
+		printf(" %d", ndigit[i]);
+	printf(". white space = %d, other = %d\n",
+			nwhite, nother);
+}
diff --git a/cat.c b/cat.c
new file mode 100644
index 0000000..eaf8cd3
--- /dev/null
+++ b/cat.c
@@ -0,0 +1,10 @@
+// from section 1.5 of K&R
+#include <stdio.h>
+
+int main() {
+	int c;
+	while ((c = getchar()) != EOF) {
+		putchar(c);
+	}
+	return 0;
+}
diff --git a/max.c b/max.c
new file mode 100644
index 0000000..ef5b331
--- /dev/null
+++ b/max.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <time.h>
+int main() {
+    char i = 0;
+    while (1) {
+        printf("%d\n", i);
+        i++;
+        sleep(1);
+    }
+    return 0;
+}
diff --git a/wc.c b/wc.c
new file mode 100644
index 0000000..e1445e1
--- /dev/null
+++ b/wc.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+int main() {
+	long nc, nw, nl;
+	int c;
+	char inWord;
+	nc = nw = nl = 0;
+	while ((c = getchar()) != EOF) {
+		if (c == '\n') ++nl;
+		if (c == ' ' || c == '\t' || c == '\n')  inWord=0;
+		else if (inWord == 0) {
+			inWord = 1;
+			++nw;
+		}
+		++nc;
+	}
+	printf("  %ld  %ld %ld\n", nl, nw, nc);
+	return 0;
+}