summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Carpenter <53hornet@gmail.com>2019-05-10 16:00:12 -0400
committerAdam Carpenter <53hornet@gmail.com>2019-05-10 16:00:12 -0400
commit2c345e0909a082b77be959a4e6166cdbdc79e067 (patch)
tree4d9ff5a2884e5b87b8c0cb8e7fa358e3a9625eb4
downloadlearning-c-2c345e0909a082b77be959a4e6166cdbdc79e067.tar.xz
learning-c-2c345e0909a082b77be959a4e6166cdbdc79e067.zip
Init.
-rw-r--r--copier.c29
-rw-r--r--counter.c29
-rw-r--r--ctof.c24
-rw-r--r--ftoc.c32
-rw-r--r--hello.c8
-rw-r--r--line_counter.c16
-rw-r--r--whitespace_counter.c22
7 files changed, 160 insertions, 0 deletions
diff --git a/copier.c b/copier.c
new file mode 100644
index 0000000..eeade8e
--- /dev/null
+++ b/copier.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+
+/* Copy input to output; 1st version */
+//main() {
+// int c;
+//
+// c = getchar();
+//
+// while (c != EOF) {
+// putchar(c);
+// c = getchar();
+// }
+//}
+
+/* Copy input to output; 2nd version */
+main() {
+ int c;
+
+ while ((c = getchar()) != EOF) {
+ putchar(c);
+ }
+}
+
+//main() {
+// printf("%d", EOF);
+// getchar();
+// printf("%d", getchar());
+// printf("%d", getchar());
+//}
diff --git a/counter.c b/counter.c
new file mode 100644
index 0000000..a1b9927
--- /dev/null
+++ b/counter.c
@@ -0,0 +1,29 @@
+#include<stdio.h>
+
+/*
+ * Count characters in input; 1st version
+ */
+//main() {
+// long nc;
+//
+// nc = 0;
+//
+// while (getchar() != EOF)
+// ++nc;
+//
+//
+// printf("%ld\n", nc);
+//}
+
+/*
+ * Count characters in input; 2nd version
+ */
+main() {
+ long nc;
+
+
+ for (nc = 0; getchar() != EOF; ++nc)
+ ;
+
+ printf("%ld\n", nc);
+}
diff --git a/ctof.c b/ctof.c
new file mode 100644
index 0000000..7e2cbef
--- /dev/null
+++ b/ctof.c
@@ -0,0 +1,24 @@
+#include<stdio.h>
+
+/*
+ * Print Celcius-Fahrenheit table for celcius = 0, 20, ... 300.
+ */
+main() {
+ float fahr, celcius;
+ int lower, upper, step;
+
+ lower = 0; // lower limit of temperature table
+ upper = 300; // upper limit of temperature table
+ step = 20; // step size
+
+ celcius = lower;
+
+ printf("%3s %6s\n", "C", "F");
+
+ while (celcius <= upper) {
+ fahr = (9.0 / 5.0) * celcius + 32.0;
+ printf("%3.0f %6.1f\n", celcius, fahr);
+ celcius = celcius + step;
+ }
+}
+
diff --git a/ftoc.c b/ftoc.c
new file mode 100644
index 0000000..2efa6f7
--- /dev/null
+++ b/ftoc.c
@@ -0,0 +1,32 @@
+#include<stdio.h>
+
+#define LOWER 0 // lower limit of table
+#define UPPER 300 // upper limit of table
+#define STEP 20 // step size
+
+/*
+ * Print Fahrenheit-Celcius table for fahr = 0, 20, ... 300.
+ */
+main() {
+ float fahr, celcius;
+ int lower, upper, step;
+
+ lower = 0; // lower limit of temperature table
+ upper = 300; // upper limit of temperature table
+ step = 20; // step size
+
+ fahr = lower;
+
+ printf("%3s %6s\n", "F", "C");
+
+ // while (fahr <= upper) {
+ // celcius = 5.0 / 9.0 * (fahr - 32.0);
+ // printf("%3.0f %6.1f\n", fahr, celcius);
+ // fahr = fahr + step;
+ // }
+
+ for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) {
+ printf("%3.0f %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));
+ }
+}
+
diff --git a/hello.c b/hello.c
new file mode 100644
index 0000000..b1ad928
--- /dev/null
+++ b/hello.c
@@ -0,0 +1,8 @@
+#include<stdio.h>
+
+main() {
+ printf("hello, ");
+ printf("world.");
+ printf("\n");
+}
+
diff --git a/line_counter.c b/line_counter.c
new file mode 100644
index 0000000..0447aa7
--- /dev/null
+++ b/line_counter.c
@@ -0,0 +1,16 @@
+#include<stdio.h>
+
+/*
+ * Count lines in input
+ */
+main() {
+ int c, nl;
+
+ nl = 0;
+
+ while ((c = getchar()) != EOF)
+ if (c == '\n')
+ ++nl;
+
+ printf("%d\n", nl);
+}
diff --git a/whitespace_counter.c b/whitespace_counter.c
new file mode 100644
index 0000000..ea441e2
--- /dev/null
+++ b/whitespace_counter.c
@@ -0,0 +1,22 @@
+#include<stdio.h>
+
+/*
+ * Count whitespace in input
+ */
+main() {
+ int c, n_whitespace;
+
+ n_whitespace = 0;
+
+ while ((c = getchar()) != EOF) {
+ if (c == '\n')
+ ++n_whitespace;
+ if (c == '\t')
+ ++n_whitespace;
+ if (c == ' ')
+ ++n_whitespace;
+ }
+
+
+ printf("%d\n", n_whitespace);
+}