summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author53hornet <53hornet@gmail.com>2019-05-11 20:12:12 -0400
committer53hornet <53hornet@gmail.com>2019-05-11 20:12:12 -0400
commit33dd87e3075790ca50f39a9ef2c1f1645e8da933 (patch)
tree41a23fd1a72ee3139f43456d0c5c893b32593fbd
parent2c345e0909a082b77be959a4e6166cdbdc79e067 (diff)
downloadlearning-c-33dd87e3075790ca50f39a9ef2c1f1645e8da933.tar.xz
learning-c-33dd87e3075790ca50f39a9ef2c1f1645e8da933.zip
Added word translator and wc.
-rw-r--r--wc.c29
-rw-r--r--whitespace_converter.c18
-rw-r--r--whitespace_translator.c26
3 files changed, 73 insertions, 0 deletions
diff --git a/wc.c b/wc.c
new file mode 100644
index 0000000..4a5007d
--- /dev/null
+++ b/wc.c
@@ -0,0 +1,29 @@
+#include<stdio.h>
+
+#define IN 1 // inside a word
+#define OUT 0 // outside a word
+
+/*
+ * Count lines, words, and characters in input.
+ */
+main() {
+ 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);
+}
diff --git a/whitespace_converter.c b/whitespace_converter.c
new file mode 100644
index 0000000..6fee3bd
--- /dev/null
+++ b/whitespace_converter.c
@@ -0,0 +1,18 @@
+#include<stdio.h>
+
+/*
+ * Convert concurrent blanks to single blank.
+ */
+main() {
+ int c;
+
+ while ((c = getchar()) != EOF) {
+ if (c == ' ') {
+ while ((c = getchar()) == ' ')
+ ;
+ putchar(' ');
+ }
+
+ putchar(c);
+ }
+}
diff --git a/whitespace_translator.c b/whitespace_translator.c
new file mode 100644
index 0000000..efb52b2
--- /dev/null
+++ b/whitespace_translator.c
@@ -0,0 +1,26 @@
+#include<stdio.h>
+
+/*
+ * Translate invisible whitespace characters to visible
+ * representations.
+ */
+main() {
+ int c;
+
+ while ((c = getchar()) != EOF) {
+ if (c == 8) {
+ printf("\\b");
+ continue;
+ }
+ if (c == 9) {
+ printf("\\t");
+ continue;
+ }
+ if (c == '\\') {
+ printf("\\\\");
+ continue;
+ }
+
+ putchar(c);
+ }
+}