Edward Bartolo <edbarx@???> writes:
> I am saving your program for future study as its level is beyond the
> current level I am at. I am still at page 34 ("The C programming
> language" (Kernighan & Ritchie))
This is based on another simple idea, namely, instead of using a simple
pattern-recognition automaton, upon encountering a ' ', use a second
loop to skip over all immediately adjacent blanks. I thought about
providing an example of that but decided against it because I considered
it needlessly using a structurally unpleasant algorithm. An more
straight-forward example of this could be:
-----------
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
do c = getchar(); while (c == ' ');
if (c != EOF) putchar(c);
}
}
return 0;
}
-----------
It's not necessary to break out of the outer loop upon encountering an
EOF in the inner one as the next getchar will again return EOF. Somewhat
interesting variant:
-----------
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
do c = getchar(); while (c == ' ');
if (c != EOF) ungetc(c, stdin);
}
}
return 0;
}
----------
This uses ungetc to stuff the character which caused the inner loop to
terminate back into the stream so that the outer loop will read it again
next.