When this piece of code encounters a space, it will eat all the spaces that follow consecutively, and print just one space.
while((c=getchar()) != EOF) { // As long as we don't reach EOF, get next character
if(c==' ') { // If character is a space,
// then, keep getting characters, as long as they are spaces
while((c=getchar()) == ' '); // This empty-bodied loop will stop when current character is not a space
putchar(' '); // Output a space character
}
}
So, if the input contains ten (for example) consecutive (next-to-each-other) spaces, it will only print one space.
Read more in K&R Exercise 1-9 (C).
need an explanation for the condition in the eating loop. the first loop lets me make an entry, so why does the second just reads c further instead of asking for a new entry?
The outer loop will read a character, and for the sake of this example, let's assume that's a space. It will enter its body, and enter if-statement's body as well.
Now, it will read for the next character (in the inner loop), and if it's a space, the code will basically do nothing with that space-character, since the body of the inner loop is empty. It will go on, asking for the next character.. It will do the same thing again and again, as long as the next consecutive character is a space.
When the next character is not a space, the code will output a space (representing all the freshly-encountered spaces).
For instance, let's assume that the user inputs three spaces, which under the hood, are stored in the Standard Input (STDIN) buffer:
----------
|''|''|''|
----------
First execution of the outer loop: Gets first character and stores it to c, makes sure that it's not EOF. Control flow of our code enters outer loop's body.
The if statement confirms that c is a space, enters the its body.
At this point, the STDIN buffer is this:
----------
|''|''| |
----------
Now, we reached the inner loop while((c=getchar()) == ' ');, which has an empty body.
getchar() method will get the next character, and store it in c. It will then check if it's a space, which is true in our case. Now, we enter the empty body of the inner loop, and we exit it right away, since there is nothing to do here.
At this point, the STDIN buffer is this:
----------
|''| | |
----------
Again, the inner loop gets executed, the method gets the next character awaiting in the buffer, and stores it in c. It'll then check if c is a space, and since this is true in this case too, and we will again enter the empty loop, do nothing and exit it.
At this point, the STDIN buffer is this:
----------
| | | |
----------
which is empty.
Once again, the inner loop gets executed, the method asks for the next character awaiting in the buffer, and stores it in c. However, since there are no more characters to read, the getchar() method returns EOF, so the check inside the inner's loop condition for the space character fails, and the control flow of our code goes to the next line of our code, since the condition of the inner loop is now false.
Next line is putchar(' ');, which outputs a single space, without caring about the number of spaces we read before, which in this case was three.
Finally, the outer loop gets executed again, for the second time, and getchar() once again asks for the next character to read, and we store its return value in c. c is equal to EOF, which results in the condition of the outer loop to be false, which means that we now exit the outer loop as well.