/* * sp2nl.c * Chapter5 * Selfttest2 * Problem 4 * John Stile * Purpose: Write a program, which converts every instance of space * characters to a newline character when the user redirects * a file or pipes the output. * use getchar and putchar in a while loop */ #include // include stdio.h functions during compile int main(void) // header for main: takes nothing, reutnrs int. { char cc; // declare char variable cc printf("Enter a sring with spaces, and press enter: "); //prompt for input while ( (cc=getchar()) != EOF ) // get character // assign to variable cc // exit loop when cc == EOL { if ( cc == ' ' ) // if char equals a space, output a new line { putchar('\n'); } else // for everything else, { putchar(cc); // output the contents of cc } } putchar('\n'); // add a final new line return 0; // return 0 to calling program }