I was trying to solve question 1.12 of k&r which asked me to write a c program that prints its input one word per line.
This is the code I wrote :
Code:
#include <stdio.h>
int main() {
int c;
while((c=getchar())!='\n')
{
if ( c == ' ' || c == '\t'){
putchar('\n');
while (c == ' ' || c== '\t')
c = getchar();
}
else {
putchar(c);
}
}
}
My problem with these lines :
Code:
while (c == ' ' || c== '\t')
c = getchar();
What's the use of this line in the program? Doesn't the code
while ((c=getchar()) != EOF) ensures that it keeps on getting char?
And why the program doesn't work if I put the putchar(c) outside the while?