commit - 7bdbee065bfa8d12eda731448540f5935d938f71
commit + c4d09d889b490e037088cc0bfd290f273ea8dca4
blob - /dev/null
blob + 988f4bbeb82ce6dc19e2e3bd99edd391fba385d3 (mode 644)
--- /dev/null
+++ 1-10.c
+/* 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. */
+
+#include <stdio.h>
+int main() {
+ int c;
+ enum { out, in } word; /* indicates whether inside a word */
+ while ((c = getchar()) != EOF) {
+ if (c == ' ' || c == '\t') {
+ if (word == in) {
+ putchar(' ');
+ }
+ word = out;
+ } else {
+ putchar(c);
+ word = in;
+ }
+ }
+}
blob - /dev/null
blob + 932b8a6f8d34a15c2115ae5066f6d6804bfbeaeb (mode 644)
--- /dev/null
+++ 1-8-2.c
+/* 1-8 Write a program to count blanks, tabs, and newlines. */
+
+#include <stdio.h>
+int main() {
+ int c;
+ int bl, tb, nl, ws;
+ bl = tb = nl = ws = 0;
+ while ((c = getchar()) != EOF) {
+ if (c == ' ') {
+ bl++; ws++;
+ } else if (c == '\t') {
+ tb++; ws++;
+ } else if (c == '\n') {
+ nl++; ws++;
+ }
+ }
+ printf("Blanks: %d, Tabs: %d, Newlines: %d, White space: %d\n", bl, tb, nl, ws);
+}
blob - /dev/null
blob + 7b7ada2f4b4c6185fe68ce5570609f972bb204aa (mode 644)
--- /dev/null
+++ 1-8-text
+"_Stoop, stoop!_" I did not understand him, till I felt my head hit against the
+beam.
blob - /dev/null
blob + 04d5d58fc12b9effe87cfcab350527ab64f6ac1c (mode 644)
--- /dev/null
+++ 1-8.c
+/* 1-8 Write a program to count blanks, tabs, and newlines. */
+
+#include <stdio.h>
+int main() {
+ int c;
+ int ws = 0;
+ while ((c = getchar()) != EOF) {
+ if (c == ' ' || c == '\t' || c == '\n') {
+ ws++;
+ }
+ }
+ printf("White space: %d\n", ws);
+}
blob - /dev/null
blob + 4af251ab0bcbebecef04a1c419ac100231e31b6b (mode 644)
--- /dev/null
+++ 1-9-2.c
+/* 1-9 Write a program to copy its input to its output, replacing each string
+ * of one or more blanks by a single blank. */
+
+#include <stdio.h>
+int main() {
+ int c;
+ enum { out, in } word; /* indicates whether inside a word */
+ while ((c = getchar()) != EOF) {
+ if (c == ' ' || c == '\t') {
+ if (word == in) {
+ putchar(' ');
+ }
+ word = out;
+ } else {
+ putchar(c);
+ word = in;
+ }
+ }
+}
blob - /dev/null
blob + 298797d256d5747f7d6a176d558d92df054d4425 (mode 644)
--- /dev/null
+++ 1-9-text
+" _Stoop, stoop!_"
+I did not understand him,
+ till I felt my head hit against the
+ beam.
blob - /dev/null
blob + a0c626dc66c6584e9904398fa769d3eeba42c1b5 (mode 644)
--- /dev/null
+++ 1-9.c
+/* 1-9 Write a program to copy its input to its output, replacing each string
+ * of one or more blanks by a single blank. */
+
+#include <stdio.h>
+int main() {
+ int c;
+ int inside = 1; /* 1 when inside a word, 0 otherwise */
+ while ((c = getchar()) != EOF) {
+ if (c == ' ' || c == '\t') {
+ if (inside == 1) {
+ putchar(' ');
+ }
+ inside = 0;
+ } else {
+ putchar(c);
+ inside = 1;
+ }
+ }
+}