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 / chlincnt.c next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  37 lines

  1. /*                     chlincnt.c
  2.  *
  3.  *   Synopsis  - Counts the number of characters and lines
  4.  *               in standard input.
  5.  *
  6.  *   Objective - Illustrates a use for strlen() in conjunction
  7.  *               with fgets().
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. /* Constant Definitions */
  15. #define BUFF_SIZE 512
  16.  
  17. int main( void )
  18. {
  19.      char inarray[BUFF_SIZE];                      /* Note 1 */
  20.      char *inptr;
  21.      int line_count = 0,                           /* Note 2 */
  22.          char_count = 0;
  23.  
  24.      printf( "Enter your text now." );
  25.      printf( "Signal end-of-file when done.\n" );
  26.      printf( "> " );
  27.                                                    /* Note 3 */
  28.      inptr = fgets( inarray, BUFF_SIZE, stdin );
  29.      while ( inptr != NULL ) {
  30.           line_count++;                            /* Note 4 */
  31.           char_count += strlen( inptr );           /* Note 5 */
  32.           printf( "> " );
  33.           inptr = fgets( inarray, BUFF_SIZE, stdin );
  34.      }
  35.      printf( "\n%d lines, %d characters\n", line_count, char_count );
  36.      return 0;
  37. }