//==================================================== file = count01.c ===== //= Program to create 0 and 1 counts of a series of 0's and 1's = //=========================================================================== //= Notes: = //= 1) Input from input file "in.dat" to stdin (see example below) = //= * Comments are not allowed = //= 2) Output is to stdout = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= = //= 0 = //= 0 = //= 1 = //= 0 = //= 0 = //= 0 = //= 1 = //= 1 = //= 0 = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat"): = //= = //= ------------------------------------------------ count01.c ----- = //= 0 2 = //= 1 1 = //= 0 3 = //= 1 2 = //= 0 1 = //= --------------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Build: bcc32 count01.c = //=-------------------------------------------------------------------------= //= Execute: count01 < in.dat = //=-------------------------------------------------------------------------= //= Author: Kenneth J. Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (11/25/02) - Genesis = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and feof() #include // Needed for atoi() //=========================================================================== //= Main program = //=========================================================================== void main(void) { int count0; // Current count of 0's int count1; // Current count of 1's char instring[80]; // String variable for input int value; // Integer value of instring // Output a banner printf("----------------------------------------------- count01.c -----\n"); // Loop to count 0's and 1's and output counts count0 = count1 = 0; while (!feof(stdin)) { // Grab the next input scanf("%s \n", instring); value = atoi(instring); // Count the 0's if (value == 0) { count0++; if (count1 > 0) printf(" 1 %3d \n", count1); count1 = 0; } // Count the 0's if (value == 1) { count1++; if (count0 > 0) printf(" 0 %3d \n", count0); count0 = 0; } } // Output any final counts and trailer if (count0 > 0) printf(" 0 %3d \n", count0); if (count1 > 0) printf(" 1 %3d \n", count1); printf("---------------------------------------------------------------\n"); }