Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** sent.c - count number of lines, words, characters & sentences from standard input.
- */
- #include <stdio.h>
- int
- main( void )
- {
- unsigned long nl = 0UL; /* #lines */
- unsigned long nw = 0UL; /* #words */
- unsigned long nc = 0UL; /* #chars */
- unsigned long ns = 0UL; /* #sentences*/
- int c;
- int inword = 0; /* true only when inside a word */
- int upper_case = 0; /* true only when character read is an Upper-case letter*/
- int period = 0; /* true only when a '.' character is read*/
- int outward = 0;
- while ( (c = getchar()) != EOF )
- {
- ++nc;
- if ( c == '\n' )
- {
- ++nl;
- }
- if ( c == ' ' || c == '\n' || c == '\t'
- || c == '\r' || c == '\v' || c == '\f' )
- {
- /* if whitespace, we're not inside a word */
- inword = 0;
- outward = 0;
- }
- else if ( !inword )
- {
- inword = 1;
- ++nw;
- }
- if ( 'A' <= c && c <= 'Z' )
- {
- upper_case = 1;
- }
- else
- {
- upper_case = 0;
- }
- if ( c == '.' || c == '?' || c == '!' )
- {
- period = 1;
- }
- if ( c == ' ' || c == '\n' || c == '\t'
- || c == '\r' || c == '\v' || c == '\f' )
- {
- /* if whitespace, we're not inside a word */
- outward = 0;
- }
- if ( period = 1 && outward == 0 && upper_case == 1) // If Uppercase letter follows a whitespace following a period
- {
- ++ns;
- period = 0; // So that sentence count is not incremented if there are capitals elsewhere in the sentence
- outward = 1;
- }
- }
- if ( period == 1)
- ++ns;
- printf( "%lu sentences, %lu lines, %lu words, %lu chars\n", ns, nl, nw, nc );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement