Exercise 5-13 - CBD page 200 - Lone Leth Thomsen

Solution index                   Textual C program


/**Solution to exercise 5.13, p. 200, "C by Dissection"
 *
 * 
 * Compresses text by removing unnecessary space
 * 
 * 07. March, Lone Leth Thomsen
 **/

#include <stdio.h>
#include <ctype.h>

int main(void)
{
        int c;


        while ((c = getchar()) != EOF) {                 /* the character is not EOF */
                if (isspace(c)){                             /* is the character a space? */
                        while ((c = getchar()) != EOF && isspace(c)) {    /* then see if the next character is not EOF and is another space */
                                ;                                             /* continue reading until not two spaces next to each other */
                        }
                        putchar(' ');                                /* all spaces cannot be removed, keep one */
                };
                putchar(c);                                  /* print the character if the first one was not a space */
        };
        return 0;
                
}
                


while ((c = getchar()) != EOF) {                 /* the character is not EOF */
the character is not EOF
 

if (isspace(c)){                             /* is the character a space? */
is the character a space?
 

while ((c = getchar()) != EOF && isspace(c)) {    /* then see if the next character is not EOF and is another space */
then see if the next character is not EOF and is another space
 

;                                             /* continue reading until not two spaces next to each other */
continue reading until not two spaces next to each other
 

putchar(' ');                                /* all spaces cannot be removed, keep one */
all spaces cannot be removed, keep one
 

putchar(c);                                  /* print the character if the first one was not a space */
print the character if the first one was not a space
 


Generated: Wednesday, March 29, 2006, 12:33:27
This program dissection page is generated from an XML-in-LAML source file