bluesky8059

Untitled

Nov 25th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. int main()
  5. {
  6.     char str[99];
  7.     char ch;
  8.     int i = 0;
  9.     int num_letter = 0;
  10.     int num_words = 0;
  11.     bool in_word = false;
  12.  
  13.     while((ch = getchar()) != '\n')
  14.     {
  15.         str[i++] = ch;
  16.     }
  17.     str[i] = '\0';
  18.  
  19.     while(*str)
  20.     {
  21.         if(*str != ' ')
  22.         {
  23.             num_letter++;
  24.             if(!in_word) //if in_word = false
  25.             {
  26.                 num_words++;
  27.                 in_word = true;
  28.             }
  29.         }
  30.         else if(in_word) //if *str == ' ', check a word is finish
  31.         {
  32.             in_word = false;
  33.         }
  34.     }
  35.  
  36.     printf("number of words: %d\n", num_words);
  37.     printf("number of letters: %d\n", num_letter);
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment