c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

count-words.c (600B)


      1 #include <stdio.h>
      2 #define IN_WORD 1
      3 #define OUT_OF_WORD 0
      4 
      5 int isNotChar(int c){
      6     if (c == ' ' || c == '\t' || c == '\n'){
      7         return 0;
      8     }
      9 
     10     return 1;
     11 }
     12 
     13 
     14 int main(){
     15     int c;
     16     int words = 0;
     17     int wordArea = OUT_OF_WORD;
     18 
     19     while((c = getchar()) != EOF){
     20         if (wordArea == IN_WORD){
     21             if(isNotChar(c) == 0){
     22                 wordArea = OUT_OF_WORD;
     23             }
     24         }
     25         else{
     26             if(isNotChar(c) == 1){
     27                 wordArea = IN_WORD;
     28                 words += 1;
     29             }
     30         }
     31     }
     32 
     33     printf("\nWord Count: %d\n", words);
     34 }