/**************************************************************** CS167 C Programming, Spring 1998 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 frm 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 any doubled space characters with single spaces. The program reads from the keyboard and writes to the screen. Your job is to complete the code 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: /*start of 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. /*end of 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 A 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 ot 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 hhuang). 7. DUE DATE: Friday, 13th., in class. 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 o The program can read single characters with the function getchar. You can write a single character using: printf("%c", character); o You will need a while loop with the termination test: (character != -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 * * with double newlines, and double spaces with single spaces * * Skeleton written by Roger Hartley, completed by _____________ * ******************************************************************/ #include int main() { /* variable declarations */ ... /* read a character */ while ... { /* read characters for the keyboard until there are no more */ if ... /* character read is a newline */ ... /* then write two newlines */ else if ... /* character read is a space and last one was NOT a space */ ... else if ... /* character read is a space and last character was a space */ ... else /* any other character */ ... ... /* remember the last character */ ... /* read another character */ } }