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 >
Wrap
C/C++ Source or Header
|
2005-06-16
|
1KB
|
37 lines
/* chlincnt.c
*
* Synopsis - Counts the number of characters and lines
* in standard input.
*
* Objective - Illustrates a use for strlen() in conjunction
* with fgets().
*/
/* Include Files */
#include <stdio.h>
#include <string.h>
/* Constant Definitions */
#define BUFF_SIZE 512
int main( void )
{
char inarray[BUFF_SIZE]; /* Note 1 */
char *inptr;
int line_count = 0, /* Note 2 */
char_count = 0;
printf( "Enter your text now." );
printf( "Signal end-of-file when done.\n" );
printf( "> " );
/* Note 3 */
inptr = fgets( inarray, BUFF_SIZE, stdin );
while ( inptr != NULL ) {
line_count++; /* Note 4 */
char_count += strlen( inptr ); /* Note 5 */
printf( "> " );
inptr = fgets( inarray, BUFF_SIZE, stdin );
}
printf( "\n%d lines, %d characters\n", line_count, char_count );
return 0;
}