Exercise 1-12 (One word per line)
Chapter_1 Exercise_1-11 | countc Exercise_1-13-1 |
Exercise 1-12 K&R, p. 21
Exercise 1-12. Write a program that prints its input one word per line.
prnwln.c download
#include <stdio.h> // for getchar(), putchar(), EOF
#define IN 1
#define OUT 0
// prints one word per line
int main()
{
int c;
int state = OUT;
while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v')
{
if (state == IN) // previous word
{
putchar('\n');
state = OUT;
}
}
else // not whitespace
{
if (state == OUT) {state = IN;}
putchar(c);
}
}
}
/*
gcc prnwln.c -o prnwln
./prnwln // input from keyboard
Hello, guys!
Hello,
guys!
How are you today?
How
are
you
today?
// Ctrl+D in Linux, Ctrl+Z then Enter in Windows (EOF)
./prnwln < prnwln.c // input from source file
./prnwln < prnwln // input from binary file
*/
Chapter_1 Exercise_1-11 | BACK_TO_TOP | countc Exercise_1-13-1 |
Comments
Post a Comment