caddy-caddy
caddy-caddy

Reputation: 205

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 to deal with whitespaces, but I don't know how not to count combinations which don't have at least one alphabetic character (I know about isalpha and isdigit, but I have difficulty in understanding how to use them in my case).

I would really appreciate your help.

Upvotes: 3

Views: 927

Answers (1)

enrico.bacis
enrico.bacis

Reputation: 31474

You can just replace:

else if (state == OUT){

with:

else if (state == OUT && isalpha(ch)){

So you set the state to IN at the first character and count it as word. Be aware that you count last.First as a single word, consider using (!isalnum(ch)) instead of (isspace(ch)).

Upvotes: 1

Related Questions