Counting words in a file in C

折月煮酒 提交于 2019-12-07 12:46:21

问题


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.


回答1:


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)).



来源:https://stackoverflow.com/questions/29772650/counting-words-in-a-file-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!