From 9ff9f305ff7059681d691d750582e6259cd90b59 Mon Sep 17 00:00:00 2001
From: zlg <zlg@zlg.space>
Date: Thu, 7 Feb 2013 02:52:52 -0600
Subject: Solve Exercises 1-1 to 1-12

It was bugging me and the exercises are simple, so I pumped'em out.
---
 1-01_hello-world.c       | 14 ++++++++++++++
 1-02_escape-sequences.c  | 15 +++++++++++++++
 1-03_temp-table-header.c | 29 +++++++++++++++++++++++++++++
 1-04_celsius_converter.c | 27 +++++++++++++++++++++++++++
 1-05_backwards-table.c   | 29 +++++++++++++++++++++++++++++
 1-06_eof-test.c          | 15 +++++++++++++++
 1-07_eof-value.c         | 13 +++++++++++++
 1-08_space-counter.c     | 29 +++++++++++++++++++++++++++++
 1-09_single-spacing.c    | 27 +++++++++++++++++++++++++++
 1-10_literal-escapes.c   | 33 +++++++++++++++++++++++++++++++++
 1-11_word-count.c        | 37 +++++++++++++++++++++++++++++++++++++
 1-12_one-word-per-line.c | 39 +++++++++++++++++++++++++++++++++++++++
 12 files changed, 307 insertions(+)
 create mode 100644 1-01_hello-world.c
 create mode 100644 1-02_escape-sequences.c
 create mode 100644 1-03_temp-table-header.c
 create mode 100644 1-04_celsius_converter.c
 create mode 100644 1-05_backwards-table.c
 create mode 100644 1-06_eof-test.c
 create mode 100644 1-07_eof-value.c
 create mode 100644 1-08_space-counter.c
 create mode 100644 1-09_single-spacing.c
 create mode 100644 1-10_literal-escapes.c
 create mode 100644 1-11_word-count.c
 create mode 100644 1-12_one-word-per-line.c

diff --git a/1-01_hello-world.c b/1-01_hello-world.c
new file mode 100644
index 0000000..e02c87a
--- /dev/null
+++ b/1-01_hello-world.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-1: Run the "hello, world" program on your system. Experiment with
+ * leaving out parts of the program, to see what error messages you get.
+ *
+ * This is the typical first exercise. No need for me to putz with it.
+ */
+
+int main(void) {
+	printf("Hello world!\n");
+	return 0;
+}
diff --git a/1-02_escape-sequences.c b/1-02_escape-sequences.c
new file mode 100644
index 0000000..8d122e0
--- /dev/null
+++ b/1-02_escape-sequences.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-2: Experiment to find out what happens when printf()'s argument
+ * string contains \c, where 'c' is some character that's not \, t, b, n, or ".
+ *
+ * Answer: This file will not (normally) compile because \d is not a valid
+ * escape sequence. Your compiler may ignore this, however.
+ */
+
+int main(void) {
+	printf("Hello world! \d\n");
+	return 0;
+}
diff --git a/1-03_temp-table-header.c b/1-03_temp-table-header.c
new file mode 100644
index 0000000..d0d069c
--- /dev/null
+++ b/1-03_temp-table-header.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-3: Modify the temperature conversion program to print a heading
+ * above the table.
+ *
+ * Answer: Just add a printf() or two before the table.
+ */
+
+int main(void) {
+	float fahr, celsius;
+	int lower, upper, step;
+
+	lower = 0;
+	upper = 300;
+	step = 20;
+
+	fahr = lower;
+
+	printf("   F |  C\n");
+	printf("------------\n");
+	while (fahr <= upper) {
+		celsius = (5.0 / 9.0) * (fahr - 32.0);
+		printf(" %3.0f %6.1f\n", fahr, celsius);
+		fahr += step;
+	}
+	return 0;
+}
diff --git a/1-04_celsius_converter.c b/1-04_celsius_converter.c
new file mode 100644
index 0000000..a397d9a
--- /dev/null
+++ b/1-04_celsius_converter.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-4: Write a program to print the corresponding Celsius to
+ * Fahrenheit table.
+ */
+
+int main(void) {
+	float fahr, celsius;
+	int lower, upper, step;
+
+	lower = 0;
+	upper = 300;
+	step = 20;
+
+	celsius = lower;
+
+	printf("   C |  F\n");
+	printf("------------\n");
+	while (celsius <= upper) {
+		fahr = (celsius * (9.0 / 5.0)) + 32.0;
+		printf(" %3.0f %6.1f\n", celsius, fahr);
+		celsius += step;
+	}
+	return 0;
+}
diff --git a/1-05_backwards-table.c b/1-05_backwards-table.c
new file mode 100644
index 0000000..a09619a
--- /dev/null
+++ b/1-05_backwards-table.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-5: Modify the temperature conversion program to print the table in
+ * reverse order, that is, from 300 degrees to 0.
+ *
+ * Answer: Ensure that fahr starts at upper and the while loop runs until zero.
+ */
+
+int main(void) {
+	float fahr, celsius;
+	int lower, upper, step;
+
+	lower = 0;
+	upper = 300;
+	step = 20;
+
+	fahr = upper;
+
+	printf("   F |  C\n");
+	printf("------------\n");
+	while (fahr >= 0) {
+		celsius = (5.0 / 9.0) * (fahr - 32.0);
+		printf(" %3.0f %6.1f\n", fahr, celsius);
+		fahr -= step;
+	}
+	return 0;
+}
diff --git a/1-06_eof-test.c b/1-06_eof-test.c
new file mode 100644
index 0000000..5214338
--- /dev/null
+++ b/1-06_eof-test.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-6: Verify that the expression 'getchar() != EOF' is 0 or 1.
+ *
+ * Answer: Easy.
+ */
+
+int main(void) {
+	int c;
+	c = (getchar() != EOF);
+	printf("%d\n", c);
+	return 0;
+}
diff --git a/1-07_eof-value.c b/1-07_eof-value.c
new file mode 100644
index 0000000..aaf838b
--- /dev/null
+++ b/1-07_eof-value.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-7: Write a program to print the value of EOF.
+ *
+ * Answer: Just cast it to an integer placeholder in printf().
+ */
+
+int main(void) {
+	printf("%d\n", EOF);
+	return 0;
+}
diff --git a/1-08_space-counter.c b/1-08_space-counter.c
new file mode 100644
index 0000000..0e00176
--- /dev/null
+++ b/1-08_space-counter.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-8: Write a program to count blanks, tabs, and newlines.
+ *
+ * Answer: Run a loop with getchar() and check its value. Then add to the
+ * counts and spit them out at the end of the loop.
+ */
+
+int main(void) {
+	char c;
+	int blanks, tabs, nls = 0;
+
+	while ((c = getchar()) != EOF) {
+		if (c == ' ') {
+			blanks++;
+		}
+		if (c == '\t') {
+			tabs++;
+		}
+		if (c == '\n') {
+			nls++;
+		}
+	}
+
+	printf("%d blanks, %d tabs, and %d newlines.\n", blanks, tabs, nls);
+	return 0;
+}
diff --git a/1-09_single-spacing.c b/1-09_single-spacing.c
new file mode 100644
index 0000000..82447ee
--- /dev/null
+++ b/1-09_single-spacing.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-9: Write a program to copy its input to its output, replacing each
+ * string of one or more blanks by a single blank.
+ *
+ * Answer: Run a loop with getchar() and check its value. If it's a space, count
+ * it, but prevent further spaces from being printed. Anything else should
+ * simply be spat out.
+ */
+
+int main(void) {
+	char c;
+	int spaces = 0;
+
+	while ((c = getchar()) != EOF) {
+		if (c == ' ' && spaces == 0) {
+			putchar(c);
+			spaces++;
+		} else {
+			putchar(c);
+		}
+	}
+
+	return 0;
+}
diff --git a/1-10_literal-escapes.c b/1-10_literal-escapes.c
new file mode 100644
index 0000000..903fff4
--- /dev/null
+++ b/1-10_literal-escapes.c
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-10: Write a program to copy its input to its output, replacing
+ * each tab by '\t', each backspace by '\b', and each backslash by '\\'. This
+ * makes tabs and backspaces visible in an unambiguous way.
+ *
+ * Answer: Run a loop with getchar() and check its value. When you run into a
+ * tab, backspace, or backslash, just output the two characters and move on.
+ */
+
+int main(void) {
+	char c;
+
+	while ((c = getchar()) != EOF) {
+		if (c == '\t') {
+			printf("\\t");
+			continue;
+		}
+		if (c == '\b') {
+			printf("\\b");
+			continue;
+		}
+		if (c == '\\') {
+			printf("\\\\");
+			continue;
+		}
+		putchar(c);
+	}
+
+	return 0;
+}
diff --git a/1-11_word-count.c b/1-11_word-count.c
new file mode 100644
index 0000000..1b4056c
--- /dev/null
+++ b/1-11_word-count.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-11: How would you test the word count program? What kinds of
+ * input are most likely to uncover bugs if there are any?
+ *
+ * Answer: New lines (such as those caused by wrapping text) are most likely to
+ * introduce erroneous word counts, as well as symbols that are strung together
+ * but don't create actual words.
+ */
+
+#define IN 1
+#define OUT 0
+
+int main(void) {
+	int c, nl, nw, nc, state;
+	state = OUT;
+	nl = nw = nc = 0;
+
+	while ((c = getchar()) != EOF) {
+		nc++;
+		if (c == '\n') {
+			nl++;
+		}
+		if (c == ' ' || c == '\n' || c == '\t') {
+			state = OUT;
+		} else if (state == OUT) {
+			state = IN;
+			nw++;
+		}
+	}
+
+	printf("%d %d %d\n", nl, nw, nc);
+
+	return 0;
+}
diff --git a/1-12_one-word-per-line.c b/1-12_one-word-per-line.c
new file mode 100644
index 0000000..3f31d1d
--- /dev/null
+++ b/1-12_one-word-per-line.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+
+/* The C Programming Language, 2nd Edition
+ *
+ * Exercise 1-12: Write a program that prints its input one word per line.
+ *
+ * Answer: Be sure to output letters when you're in a word, and a newline when
+ * you're out of one.
+ */
+
+#define IN 1
+#define OUT 0
+
+int main(void) {
+	int c, nl, nw, nc, state;
+	state = OUT;
+	nl = nw = nc = 0;
+
+	while ((c = getchar()) != EOF) {
+		nc++;
+		if (c == '\n') {
+			nl++;
+		}
+		if (c == ' ' || c == '\n' || c == '\t') {
+			state = OUT;
+			putchar('\n');
+		} else if (state == OUT) {
+			state = IN;
+			nw++;
+		}
+		if (state == IN) {
+			putchar(c);
+		}
+	}
+
+	printf("%d %d %d\n", nl, nw, nc);
+
+	return 0;
+}
-- 
cgit v1.2.3-54-g00ecf