Solution for Exercise 13-17

ex1317

#include <stdio.h>

int main()
{
    char i;

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

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

Notes

* 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. Your 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 ex1317. If so, here's how command prompt input redirection would work:

Above, the text from the source code file ex1317.c is directed into the program file ex1317. 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.