Advertisement
regzarr

problem 2

Oct 16th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. /* Paragraph  info:
  5.  * Write  a  program  that  reads  all  input  and  prints  for  each  paragraph
  6.  * the  number  of  words  and  lines.  A  paragraph  is  a  portion  of  text
  7.  * that  does  not  start with  a  newline,  ends  with  a  single  newline  or
  8.  * EOF  and  is  separated  by  at  least  one newline  from  other  paragraphs.
  9.  */
  10.  
  11. void paragraphInfo(){
  12.   char a, b;
  13.   unsigned wordCount = 0, lineCount = 0;
  14.   int spaceBeforeWord = 0;
  15.   a = getchar();
  16.   while((b = getchar()) != EOF){
  17.     if(isspace(a) && !isspace(b)){
  18.       spaceBeforeWord = 1;
  19.     }
  20.     if(isspace(b) && !isspace(a) && spaceBeforeWord){
  21.       wordCount++;
  22.     }
  23.     if(b == '\n' && a != '\n'){
  24.       lineCount++;
  25.     }
  26.     if(b == '\n' && a == '\n'){
  27.       printf("words: %u, lines: %u\n", wordCount, lineCount);
  28.       wordCount = 0;
  29.       lineCount = 0;
  30.     }
  31.     a = b;
  32.   }
  33. }
  34.  
  35. int main(){
  36.   paragraphInfo();
  37.   return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement