Exercise 5-6 - CBD page 198 - Lone Leth Thomsen

Solution index                   Textual C program


/**Solution to exercise 5.6, p. 198, "C by Dissection"
 *
 * Counts the number of characters as they are written. When the count is N or more
 *  write any white space as newline and change the count to zero.
 * 
 *
 * 07. March, Lone Leth Thomsen
 **/


#include <stdio.h>
#include <ctype.h>
#define N 30                                            /* defines the width */

int found_next_char(void);

int main(void)
{
        int c;
        int char_count = 0;

        while ((c = getchar()) != EOF) {                    /* checks that the character is not EOF */
                ++char_count;                               /* increases counter */
                if (char_count < N){                        /* checks width is less than N */
                        if (c != '\n')                      /* checks the character is not newline */
                                putchar(c);                 /* writes character */
                        
                        else 
                                putchar(' ');               /* writes space if the character was newline */
                        
                }
                else { 
                        if (!isspace(c))                    /* if width reached check that the character is not a space */
                                putchar(c);
                                
                        else {
                                putchar('\n');              /* if space found, add newline and reset count to zero */
                                char_count = 0;
                             }
                        }
        }
                

                
                
        return 0;
}


#define N 30                                            /* defines the width */
defines the width
 

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

                ++char_count;                               /* increases counter */
increases counter
 

                if (char_count < N){                        /* checks width is less than N */
checks width is less than N
 

                        if (c != '\n')                      /* checks the character is not newline */
checks the character is not newline
 

                                putchar(c);                 /* writes character */
writes character
 

                                putchar(' ');               /* writes space if the character was newline */
writes space if the character was newline
 

                        if (!isspace(c))                    /* if width reached check that the character is not a space */
if width reached check that the character is not a space
 

                                putchar('\n');              /* if space found, add newline and reset count to zero */
if space found, add newline and reset count to zero
 


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