I have a program written in C, that should count words in a given file. The program count the number of words in file, but not the exactly count (this situation perform when there are multiple lines to read in a file, so when he reads \n I guess). Can you explain me why the count is wrong? That's my code:
int in_word = 1;
while ((ch = fgetc(fp)) != EOF) {
     if(ch == ' ' || ch == '\t' || ch == '\0' || ch == '\n') {
            if (in_word) {
                  in_word = 0;
                  word_count++;
                  }
            } else {
                      in_word = 1; 
                    }
             
       }
If I try with in_word = 0; the count is correct-2. Can you explain me why?
The file: hello hello hello hello hello hello hello (\n here) hi hi hi hi hi hi
Output: 12 words Correct is: 13
 
     
    