I'm trying to make my version of the following C program contained in a famous book of C programming language:
Write a program that print its input one word per line. Here's my code:
#include <stdio.h>
#define OUT 1
#define IN 0
int main(void){
char c;
int state = OUT; /*Indicates if we are IN a white space, tab, nl or if we are OUT (a simple letter)*/
while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\n' || c == '\t')
{
state = IN;
putchar('\n');
} else
if(state == IN){
state = OUT;
putchar(c);
}
}
return 0;
}
To test it, I tried as input:
- "abc" which the program output as "a".
- "a b c" which the program output as one letter per line.
It is correct to say, that "abc" was trunked because of getchar() function, which memorize just one character in "c" variable? And it is also correct, say that "a b c" were being processed 3 times (3 while iteration) by getchar() function as single char because of a space between every letter?