I'm doing K&R C Exercises. I could use a code review to see how I can improve my logic. The excercise is:
Write a program that prints its input one word per line with
getchar
.
My basic idea is we need to know whether or not we're in a word. We use the IN status. Every time we hit a blank space, tab, or newline, the status is changed to OUT.
Please offer only suggestions based on what i'm learning in the book up to this point, "symbolic constants, if else, and loops only." Also thoughts on my comments would be welcome.
// Exercise1.11.cpp : Defines the entry point for the console application.
/*
Write a program that prints its input one word per line.
*/
#include "stdafx.h"
#define IN 1
#define OUT 0
int _tmain(int argc, _TCHAR* argv[])
{
int status = OUT;
int c;
while ((c = getchar()) != EOF){
// Check to see if char is not a new line, blank or space.
if ((c != '\n' && c !=' ' && c != '\t') ){
if (status == OUT){
putchar('\n');
status = IN;
}
putchar(c);
}
else {
// char is a new line, blank or tab, so don't process them.
status = OUT;
}
}
return 0;
}