The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 1.12 on page 21

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

Solution by Richard Heathfield; corrected by FF.

#include <stdio.h>

int main(void)
{
    int c;
    int inspace;

    inspace = 1;
    while ((c = getchar()) != EOF)
    {
        if (c == ' ' || c == '\t' || c == '\n')
        {
            if (inspace == 0)
            {
                inspace = 1;
                putchar('\n');
            }
            /* else, don't print anything */
        }
        else
        {
            inspace = 0;
            putchar(c);
        }
    }

    return 0;
}

Personal tools
Tidy_icons
not logged in