{"content":{"id":301,"title":"K\u0026R Solutions - 1.12","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a program that prints its input one word per line.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\nint main(void)\r\n{\r\n char c;\r\n\r\n while ((c = getchar()) != EOF) {\r\n if (c == '\\t' || c == '\\n' || c == ' ') {\r\n printf(\"\\n\");\r\n } else {\r\n putchar(c);\r\n }\r\n }\r\n\r\n return 0;\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e\u0026gt muh muh muh moo cows\r\nmuh\r\nmuh\r\nmuh\r\nmoo\r\ncows\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-10-05T01:00:00.000Z","created_at":"2018-10-04T23:41:25.000Z","updated_at":"2018-10-05T03:44:39.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=k%26r\"\u003ek\u0026amp;r\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=unix\"\u003eunix\u003c/a\u003e"}

K&R Solutions - 1.12

Q: Write a program that prints its input one word per line.

A:

#include <stdio.h>

int main(void)
{
  char c;

  while ((c = getchar()) != EOF) {
    if (c == '\t' || c == '\n' || c == ' ') {
      printf("\n");
    } else {
      putchar(c);
    }
  }

  return 0;
}
> muh muh muh     moo cows
muh
muh
muh
moo
cows