/**************************************************************** CS167/467 C Programming, Fall 1999 Lab 3. Control structures The goal is to feel comfortable with the basic control structures. The assignment is to use the conditional form and a while loop. YOU MAY NOT BE ABLE TO COMPLETE THIS ASSIGNMENT (WORTH 20 POINTS) IN THE LAB SESSION, SO BE PREPARED TO FINISH IT IN YOUR OWN TIME. 1. Edit the source code from this message exactly as in your first assignment. 2. The code below is a skeleton for a program that reads any number of characters from the keyboard, and replaces all of the newline characters with double newlines, thus making the file double-spaced. In addition, it replaces multiple consecutive space characters with a single space. The program reads from the keyboard and writes to the screen. Your job is to complete the code by writing suitable statements and/or expressions to replace the ...'s in order to achieve the task just described. 3. Compile and run the program several times to test its results. 4. Run the program once more with this input: /*next line is the start of the input file*/ Here is some input, spread over several lines with two instances of doubled spaces that need to be reduced to one space each time. /*the previous line was the end of the input*/ (Don't include the comment lines in your input file) You will need to save the four lines of the input in a file and use INPUT redirection to get the program to read the text: % a.out < input.file > output.file As usual, insert the output file into a copy of the souce code. 5. Print the file with the source code and output and get it to me (RTH). You can give it to me in class, or put it in my mailbox (SH123 is the CS departmental office). 6. Mail the same file to our grader (login byao). 7. DUE DATE: Friday, 17th., by 5pm. Points to note: o Don't forget to put your name in the file comment, and change the comment appropriately for your source code. o The program can read single characters with the function getchar, thus: int ch; ch = getchar(); You can write a single character to the output using putchar, thus: putchar(ch); o You will need a while loop with the termination test: (ch != -1) since getchar returns -1 when there are no more characters to read. o To tell the program there are no more characters, type a ctrl-D. You will not need to do this with the redirected input file, since UNIX effectively puts one in for you when it reaches the end of the file. o In order to detect two spaces, use a variable to store the last character read. If the current character and the last one were both spaces, then you have detected a double space. You may want to use a compound test using the 'and' operator && (see section 4.10, page 122) although nested if statements will also work well. o The skeleton program gives (in comments) the different cases your program should test for. This may not be the exact way your program should work, so feel free to change it. o TRY THE TASK FIRST ON PAPER TO FIGURE OUT A GOOD METHOD. *******************************************************************/ /****************************************************************** * This program reads any number of lines of text, replacing each * * newline with double newlines, and any number of spaces with a * * single space. * * Skeleton written by Roger Hartley, completed by _____________ * ******************************************************************/ #include int main() { /* variable declarations */ ... /* read a character from the input*/ while (...) { /* read characters for the input until there are no more */ if (...) /* character read is a newline */ ... /* then write two newlines to the output*/ else if (...) /* character is a space and last one was NOT a space */ ... else if (...) /* character is a space and last character was a space */ ... else if (...) /* character is NOT a space and last was a space */ ... else if (...) /* character is NOT a space and last was NOT a space */ ... ... /* remember the character just read as the last one */ ... /* read another character from the input*/ } return 0; }