c - 从文本文件中逐行查找特定单词的出现次数

标签 c find-occurrences

我正在尝试逐行读出我的文本文件

FILE *infile;
char line[1000];
infile = fopen("file.txt","r");
while(fgets(line,1000,infile) != NULL) 
{
    //....
}
fclose(infile);

然后我需要找到一个特定的单词,例如“the”,并且需要查看它出现了多少次以及它也出现在哪一行。

我应该能够用这个来数字数

int wordTimes = 0;
if((strcmp("the", currentWord) == 0)) 
{
    printf("'%s' appears in line %d  which is: \n%s\n\n", "the", line_num, line);
    wordTimes++;
}

其中 line 是字符串出现的文本行,line_num 是字符串出现的行号。

然后单词显示的次数使用以下代码:

if(wordTimes > 0)
{
    printf("'%s' appears %d times\n", "the", wordTimes);
}
else
{
    printf("'%s' does not appear\n", "the");
}

问题是我不确定如何将行中的每个单词与“the”进行比较,并仍然打印出它适用的行。

为此,我必须使用非常基本的 C 语言,因此这意味着我无法使用 strtok()strstr()。我只能使用 strlen()strcmp()

最佳答案

也许你需要写一个strword()像这样的功能。我假设您可以使用 <ctype.h> 中的分类函数(宏) ,但如果不允许的话,也有解决方法。

#include <assert.h>
#include <ctype.h>
#include <stdio.h>

char *strword(char *haystack, char *needle);

char *strword(char *haystack, char *needle)
{
    char *pos = haystack;
    char old_ch = ' ';
    while (*pos != '\0')
    {
        if (!isalpha(old_ch) && *pos == *needle)
        {
            char *txt = pos + 1;
            char *str = needle + 1;
            while (*txt == *str)
            {
                if (*str == '\0')
                    return pos;     // Exact match at end of haystack
                txt++, str++;
            }
            if (*str == '\0' && !isalpha(*txt))
                return pos;
        }
        old_ch = *pos++;
    }
    return 0;
}

int main(void)
{
    /*
    ** Note that 'the' appears in the haystack as a prefix to a word,
    ** wholly contained in a word, and at the end of a word - and is not
    ** counted in any of those places. And punctuation is OK.
    */
    char haystack[] =
        "the way to blithely count the occurrences (tithe)"
        " of 'the' in their line is the";
    char needle[] = "the";

    char *curpos = haystack;
    char *word;
    int count = 0;
    while ((word = strword(curpos, needle)) != 0)
    {
        count++;
        printf("Found <%s> at [%.20s]\n", needle, word);
        curpos = word + 1;
    }

    printf("Found %d occurrences of <%s> in [%s]\n", count, needle, haystack);

    assert(strword("the", "the") != 0);
    assert(strword("th", "the") == 0);
    assert(strword("t", "t") != 0);
    assert(strword("", "t") == 0);
    assert(strword("if t fi", "t") != 0);
    assert(strword("if t fi", "") == 0);
    return 0;
}

运行时,会产生:

Found <the> at [the way to blithely ]
Found <the> at [the occurrences (tit]
Found <the> at [the' in their line i]
Found <the> at [the]
Found 4 occurrences of <the> in [the way to blithely count the occurrences (tithe) of 'the' in their line is the]

Is there a way to do the strword function without <ctype.h>?

是的。我在开头段落中就说了同样的话。由于唯一使用的函数/宏是 isalpha() ,您可以做出一些假设(您不在使用 EBCDIC 的系统上),以便拉丁字母是连续的,并且您可以使用此 is_alpha()代替isalpha() - 并省略<ctype.h>从包含的 header 列表中:

static inline int is_alpha(int c)
{
    return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}

关于c - 从文本文件中逐行查找特定单词的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408721/

相关文章:

c - 段错误?你能找到吗?因为我不能

c - 练习6-4 C 语言编程

MySQL : replace occurence of a string in field except first one

Python:返回字符串中仅出现一次的单词

c++ - 查找整数数组中数字的最大出现次数

c - 没有功能指针的状态机

c - C 中允许的静态数组的最大大小是多少?

c - pthreads,我如何使用不同的类型?

c++ - 查找子字符串出现的次数

r - 数据表特殊出现次数统计