Advertisement
rafikamal

Word Count

Jan 25th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.50 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define SIZE 100
  4. #define IN 1
  5. #define OUT 2
  6.  
  7. int word_count(char str[]);
  8.  
  9. int main()
  10. {
  11.     char str[SIZE];
  12.    
  13.     gets(str);
  14.            
  15.     printf("%d\n", word_count(str));
  16.    
  17.     return 0;
  18. }
  19.  
  20. int word_count(char str[])
  21. {
  22.     int i, state = OUT, count = 0;
  23.    
  24.     for(i = 0; str[i] != '\0'; i++)
  25.     {
  26.         if((str[i] >= 'a' && str[i] <= 'z' ) || (str[i] >= 'A' && str[i] <= 'Z'))
  27.         {
  28.             if(state == OUT)
  29.             {
  30.                 state = IN;
  31.                 count++;
  32.             }
  33.         }
  34.         else
  35.             state = OUT;
  36.     }
  37.    
  38.     return count;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement