Quote:
Originally Posted by answerme
Code:
#define IN 1
#define OUT 0
main()
{
int c, state;
state = OUT;
while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\n' || c == '\t')
{
if (state == IN)
{
putchar('\n');
state = OUT;
}
}
else if (state == OUT)
{
state = IN;
putchar(c);
}else
putchar(c);
}
}
i am taking the old example, say you have entered "Hello go 4 expert forum"
Now the State is set to OUT or 0 first, the first character encountered is H so it directly goes to else if statement (as the state=OUT initially) now the state is set to IN or 1 and prints the H. Next the 'e' is encountered but the state is set to 1 so it goes to last else statement (as all if fails) and prints the character. When the first ' ' blankspace is encountered the program enters the first if blocks there it is checked whether the state is IN or not (for reason keep reading) if the state is IN (as in the taken example) the program prints one newline character and set teh state to OUT,
Now why this IN and OUT??
see if we would have taken an example like " Hello go 4 expert" i.e. with lots of space within it, then what would happen
First it will encounter a blank space so the program enters the first block of if statement, but the state is set to OUT at first step ..... so the nested if statement fails within first if statement, first space is encountered and it's not printed on the screen ...... similarly it passes all the spaces and prints the first character i.e. 'H',
if we would not use this IN & OUT then the output of the second input will be
Hello go 4 expert
Hello
go
4
expert
with IN and OUT it will print normally
Hello
go
4
expert
(though in some new compiler you wont see any difference)