Solution for Exercise 13-16

ex1316

#include <stdio.h>

int main()
{
    int i;

    do
    {
        i = getchar();
        putchar(i);
    } while(i != '.');

    putchar('\n');
    return(0);
}

Output

This is a test. It's only a test.
This is a test.

Notes

* Variable i is declared as an integer because this is the data type the getchar() and putchar() functions expect. You could use a char variable, but int is proper.

* I used a do-while loop to ensure that the statements in the loop run at least once.

* While it appears as though getchar() waits to read a character, it does not. It reads the stream. Input is merely processed one character at a time by getchar().

* If you have access to a command prompt, you can demonstrate stream input by redirecting text from a file into the program. Assuming that the code from this Exercise generates a program named ex1316. If so, here's how command prompt input redirection would work:

Above, the text from the source code file ex1316.c is redirected into the program file ex1316. Here's what the output looks like:

All the file's text was shot through the program. After the first period was encountered, output was stopped. That exactly how the program is supposed to behave thanks to stream input.