summaryrefslogtreecommitdiff
path: root/ch01/ex12-print.c
diff options
context:
space:
mode:
authorZhineng Li <[email protected]>2026-01-10 16:37:39 +0800
committerZhineng Li <[email protected]>2026-01-10 16:37:39 +0800
commit49891f6bce29843f503b437c84d83cb787ecfa87 (patch)
tree50bd44422a907b869a6d4839df436f37a35be36d /ch01/ex12-print.c
downloadc-knr-exercises-49891f6bce29843f503b437c84d83cb787ecfa87.tar.gz
c-knr-exercises-49891f6bce29843f503b437c84d83cb787ecfa87.zip
first commit
Diffstat (limited to 'ch01/ex12-print.c')
-rw-r--r--ch01/ex12-print.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/ch01/ex12-print.c b/ch01/ex12-print.c
new file mode 100644
index 0000000..91aafa2
--- /dev/null
+++ b/ch01/ex12-print.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+#define IN 1 /* inside a word */
+#define OUT 0 /* outside a word */
+
+/* print one word per line from the input */
+int main()
+{
+ int c, state;
+
+ state = OUT;
+ while ((c = getchar()) != EOF) {
+ if (c == ' ' || c == '\n' || c == '\t') {
+ if (state == IN)
+ putchar('\n');
+ state = OUT;
+ } else if (state == OUT)
+ state = IN;
+ if (state == IN)
+ putchar(c);
+ }
+ putchar('\n');
+}