Advertisement
Lisaveta777

Prata exs 8.4

Aug 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. //calculate how many words and symbols there are in file, dont count space symbols
  5.  
  6. int main(void)
  7. {
  8.     int words, symbols = 0;//words count, symbols count
  9.     char c,prev;           //current char, previous char
  10.     words = 0;
  11.     bool inword = 0;       //
  12.  
  13.     while((c = getchar()) != EOF)
  14.     {
  15.         if(inword)
  16.         {
  17.             if(isspace(c))//just got out of word
  18.                 inword = 0;
  19.             else          //keep being in word
  20.                 symbols++;
  21.         }
  22.         else
  23.         {
  24.             if(!isspace(c))//just got into word
  25.             {
  26.                 words++;
  27.                 inword =1;
  28.                 symbols++;
  29.             }
  30.             //keep being out of word, nothing to be done,no operators
  31.         }
  32.  
  33.         prev = c;
  34.     }
  35.     printf("words %d, symbols %d\n",words,symbols);
  36.  
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement