------------------------------------------------------------------------------
MC logo
Indent and Blank Remover
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Hello, World! (Plain C)] [Average C I/O] [Printf Demo] [Problems With C I/O] [Looping On Read] [Convert To Upper Case] [Indent and Blank Remover] [Reading Lines] [Safer Line Reading] [Printing C++ Strings]
nodent.c
/* This program removes any indentation and blank lines from its input. */

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

int main() 
{
        int inch;               /* Input character. */
        int suppressing = 1;    /* Currently suppressing leading space. */

        while((inch = getchar()) != EOF) {
                /* At the end of a line, if we have suppressed up to this
                   point, squash the newline, too, to eliminate the whole
                   line.  Otherwise, begin suppression for the next line. */
                if(inch == '\n')
                        if(suppressing)
                                continue;
                        else
                                suppressing = 1;
                else
                        /* Not end-of-line.  See if we are worrying about
                           suppressing spaces right now. */
                        if(suppressing) 
                                /* We are suppressing spaces.  If it's a space,
                                   squash it; if not, we aren't suppressing
                                   anymore. */
                                if(isspace(inch))
                                        continue;
                                else
                                        suppressing = 0;

                /* If we got here, this character should not be suppressed. */
                putchar(inch);
        }
}

This program processes its input one character at time, an keeps some history to help it know how to process each character.