From 33dd87e3075790ca50f39a9ef2c1f1645e8da933 Mon Sep 17 00:00:00 2001 From: 53hornet <53hornet@gmail.com> Date: Sat, 11 May 2019 20:12:12 -0400 Subject: Added word translator and wc. --- wc.c | 29 +++++++++++++++++++++++++++++ whitespace_converter.c | 18 ++++++++++++++++++ whitespace_translator.c | 26 ++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 wc.c create mode 100644 whitespace_converter.c create mode 100644 whitespace_translator.c 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 + +#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 + +/* + * 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 + +/* + * 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); + } +} -- cgit v1.2.3