home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch5 / counter.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  84 lines

  1. /*                               counter.c
  2.  *
  3.  *   Synopsis  - Reads standard input and keeps a count of the
  4.  *               number of extra blanks in the input.
  5.  *
  6.  *   Objective - To illustrate a loop to read through many
  7.  *               lines of input and a method of having a
  8.  *               function change a pointer parameter.
  9.  */
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. /* Constant Definitions */
  14. #define BUF_SIZE 512
  15.  
  16. /* Function Prototypes*/
  17. int countem( char ** ptr );                            /* Note 1 */
  18. /* PRECONDITION:  *ptr points to a space in a null terminated 
  19.  *                    string.
  20.  * POSTCONDITION: *ptr points to the first non-space character 
  21.  *                after the initial space. The number of spaces 
  22.  *                found at that location is returned.
  23.  */
  24.  
  25. int process_line(char *ptr );
  26. /* PRECONDITION:  ptr points to the beginning of the buffer 
  27.  *                containing the line to be processed.
  28.  * POSTCONDITION: Tne number of "extra" spaces in the processed 
  29.  *                line is returned.
  30.  */
  31.  
  32. int main( void )
  33. {
  34.      char inarray[BUF_SIZE], *inptr;                   /* Note 2 */
  35.     int  white_count = 0;
  36.  
  37.     printf( "Enter text. Signal end of file when done.\n" );
  38.     printf( "> " );
  39.     inptr = fgets( inarray, BUF_SIZE, stdin );         /* Note 3 */
  40.     while ( inptr != NULL ) {
  41.                                                        /* Note 4 */
  42.         white_count += process_line( inptr );
  43.         printf( "> " );
  44.         inptr = fgets( inarray, BUF_SIZE, stdin );     /* Note 3 */
  45.     }
  46.     printf( "There were %d unnecessary spaces.\n", white_count );
  47.  
  48.     return 0;
  49. }
  50.  
  51. /*******************************process_line()*****************/
  52. /*  Processes a line of text by looking for blanks, calling
  53.  *  countem() to count the blanks, and updating the running
  54.  *  total of the number of blanks.
  55.  */
  56. int process_line( char *ptr )
  57. {
  58.     int count;
  59.     int extra_whites = 0;
  60.  
  61.     while ( *ptr != '\0' ) {
  62.         if ( *ptr == ' ' ) {
  63.             count = countem( &ptr );                   /* Note 5 */
  64.             if ( count >= 2 )
  65.                 extra_whites += count - 1;
  66.          }
  67.          else
  68.              ptr++;
  69.     }
  70.     return extra_whites;
  71. }
  72.  
  73. /*******************************countem()***********************/
  74.  
  75. int countem( char **ptr )                              /* Note 6 */
  76. {
  77.     int counter = 0;
  78.     while ( **ptr == ' ' ) {                           /* Note 7 */
  79.         counter++;
  80.         ( *ptr )++;                                    /* Note 8 */
  81.         }
  82.     return( counter );
  83. }
  84.