summaryrefslogtreecommitdiff
path: root/one/char.c
diff options
context:
space:
mode:
Diffstat (limited to 'one/char.c')
-rw-r--r--one/char.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/one/char.c b/one/char.c
new file mode 100644
index 0000000..91cfc75
--- /dev/null
+++ b/one/char.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+
+#define IN 1 /* inside of a word */
+#define OUT 0 /* outside of a word */
+
+/* count lines, words, and characters */
+void
+wc()
+{
+ /*
+ * c must be big enough to hold any value getchar() returns so we use
+ * int instead of char to hold EOF, an integer defined in stdio
+ */
+ int c, nl, nw, nc, state;
+ state = OUT;
+ nl = nw = nc = 0;
+ while ((c = getchar()) != EOF) {
+ ++nc;
+ if (c == '\n') {
+ ++nl;
+ }
+ /* && is higher precedence than || */
+ if (c == ' ' || c == '\t' || c == '\n') {
+ state = OUT;
+ } else if (state == OUT) {
+ state = IN;
+ ++nw;
+ }
+ }
+ printf("%d %d %d\n", nl, nw, nc);
+}
+
+/* print input separated by new lines */
+void
+print_word_line()
+{
+ int c;
+ while ((c = getchar()) != EOF) {
+ if (c == ' ' || c == '\t' || c == '\n') {
+ putchar('\n');
+ } else {
+ putchar(c);
+ }
+ }
+}
+
+int
+main()
+{
+ wc();
+ /* print_word_line(); */
+ return 0;
+}