Counting words in a file in C
I'm writing a function that counts the number of words in a file. Words may be separated by any amount of whitespace characters. There can be integers in a file, but the program should only count words which have at least one alphabetic character. int word_count(const char *filename) { int ch; int state; int count = 0; FILE *fileHandle; if ((fileHandle = fopen(filename, "r")) == NULL){ return -1; } state = OUT; count = 0; while ((ch = fgetc(fileHandle)) != EOF){ if (isspace(ch)) state = OUT; else if (state == OUT){ state = IN; ++count; } } fclose(fileHandle); return count; } I figured out how