Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 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.            
  35.             }
  36.         else if ( !inword )
  37.             {
  38.                 inword = 1;
  39.                 ++nw;
  40.             }
  41.        
  42.         if ( 'A' <= c  &&  c  <= 'Z' )
  43.             {
  44.                 upper_case = 1;
  45.             }
  46.         else
  47.             {
  48.                 upper_case = 0;
  49.             }
  50.        
  51.         if ( c == '.' || c == '?' || c == '!' )
  52.         {
  53.             period = 1;
  54.             }
  55.        
  56.        
  57.         if ( c == ' ' || c  == '\n' || c == '\t'
  58.             || c == '\r' || c == '\v' || c == '\f' )
  59.         {
  60.             /* if whitespace, we're not inside a word */
  61.             outward = 0;
  62.            
  63.         }
  64.        
  65.        
  66.        
  67.         if ( period = 1 && outward == 0 && upper_case == 1) // If Uppercase letter follows a whitespace following a period
  68.         {
  69.             ++ns;
  70.             period = 0;    // So that sentence count is not incremented if there are capitals elsewhere in the sentence
  71.             outward = 1;
  72.         }  
  73.     }
  74.    
  75.     if ( period == 1)
  76.         ++ns;
  77.    
  78.     printf( "%lu sentences, %lu lines, %lu words, %lu chars\n", ns, nl, nw, nc );
  79.    
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement