Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. /*
  2.  ** sent.c - count number of lines, words, characters & sentences from standard input.
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int
  8. main( void )
  9. {
  10.     unsigned long  nl = 0UL;   /* #lines */
  11.     unsigned long  nw = 0UL;   /* #words */
  12.     unsigned long  nc = 0UL;   /* #chars */
  13.     unsigned long  ns = 0UL;   /* #sentences*/
  14.    
  15.     int   c;
  16.     int   inword = 0;         /* true only when inside a word */
  17.     int   upper_case = 0;     /* true only when character read is an Upper-case letter*/
  18.     int   period = 0;         /* true only when a '.' character is read*/
  19.     int   outward = 0;
  20.     while ( (c = getchar()) != EOF )
  21.     {
  22.         ++nc;
  23.        
  24.         if ( c == '\n' )
  25.             {
  26.                 ++nl;
  27.             }
  28.        
  29.         if ( c == ' ' || c  == '\n' || c == '\t'
  30.             || c == '\r' || c == '\v' || c == '\f' )
  31.             {
  32.                 /* if whitespace, we're not inside a word */
  33.                 inword = 0;
  34.                 outward = 0;
  35.            
  36.             }
  37.         else if ( !inword )
  38.             {
  39.                 inword = 1;
  40.                 ++nw;
  41.             }
  42.        
  43.         if ( 'A' <= c  &&  c  <= 'Z' )
  44.             {
  45.                 upper_case = 1;
  46.             }
  47.         else
  48.             {
  49.                 upper_case = 0;
  50.             }
  51.        
  52.         if ( c == '.' || c == '?' || c == '!' )
  53.         {
  54.             period = 1;
  55.             }
  56.        
  57.        
  58.         if ( c == ' ' || c  == '\n' || c == '\t'
  59.             || c == '\r' || c == '\v' || c == '\f' )
  60.         {
  61.             /* if whitespace, we're not inside a word */
  62.             outward = 0;
  63.            
  64.         }
  65.        
  66.        
  67.        
  68.         if ( period = 1 && outward == 0 && upper_case == 1) // If Uppercase letter follows a whitespace following a period
  69.         {
  70.             ++ns;
  71.             period = 0;    // So that sentence count is not incremented if there are capitals elsewhere in the sentence
  72.             outward = 1;
  73.         }  
  74.     }
  75.    
  76.     if ( period == 1)
  77.         ++ns;
  78.    
  79.     printf( "%lu sentences, %lu lines, %lu words, %lu chars\n", ns, nl, nw, nc );
  80.    
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement