I am working through the K&R and just finished exercise 1-12 and below is my solution. Exercise1-12: Write a program that prints input one word per line.
#include <stdio.h>
main()
{
int c;
while((c = getchar()) != EOF)
{
if(c == ' ' || c == '\t' || c == '\n')
{
putchar('\n');
while(c == ' '|| c == '\t' || c == '\n')
c = getchar();
}
putchar(c);
}
}
My question is both author and solutions here defined and used the state variable. when we compare my solution is pretty simple but it works. am I missing something? maybe there is a case that my solution won't work well?
Looking for your comments to improve, Thanks