0

我想编写一个代码来计算参数在输入中出现的频率。这些是要求:

It may be assumed
that the lines in the input do not exceed 1024 characters. The string #EOF on the beginning of a line indicates the end of the input. It it not necessary to consider word
boundaries, and overlapping words must be counted as well: with an input of baaaab,
the word aa shall be counted three times. Also, the program must be case sensitive.

我已经写了一个代码,但我似乎犯了一些错误。有人有想法吗?

int main(int argc, char *argv[])
{
    char buf[1026]="start";
    int count=0;


    while (strncmp(buf,"#EOF",4)!=0)
    {
        fgets(buf, 1025, stdin);
        if (strncmp(buf, argv[1], strlen(argv[1]))==0)
            {
                count++;

            }


    }
    if(argc==1)
        printf("Please specify a program argument.");

    if(argc>=2)
        printf("%d", count);


    return 0;
}

这是带有参数 let 的程序输入:

Let it be, let it be, let it be, let it be. 
Whisper words of wisdom, let it be. 
#EOF

并且应该是 4 时没有输出

这是带有参数 aa 的程序输入:

aa aaaaa aa
aa aaa
#EOF

输出是 2 而应该是 9

这是带有参数 EOF 的程序输入:

De volgende EOF behoort ook tot de invoer: EOF 
# Net als deze #EOF. Maar hieronder niet meer.
#EOF

并且应该是 3 时没有输入

提前致谢

4

3 回答 3

1

strncmp()n测试提供的每个字符串的第一个字符是否完全相等。但是,您想要计算每次出现的次数,而不仅仅是行首是否匹配。例如,如果您正在寻找"let"in ,那么您只是在"Let it be, let it be, let it be, let it be."测试. 没有匹配,没有计数。您永远不会进一步测试字符串。"Let""let"

因此,您要做的是循环遍历 的结果fgets(),如下所示:

    fgets(buf, 1025, stdin);
    for (char *p = buf; *p; ++p) {
        if (strncmp(p, argv[1], strlen(argv[1])) == 0)
        {
            count++;
        }
    }

这将"let"针对"Let", then "et ", then"t i"等进行测试,直到您检查了整行并计算了匹配项。

如果您要使用strstr()而不是strncmp(),则循环将如下所示:

    for (char *p = buf; (p = strstr(p, argv[1])); ++p)
    {
        count++;
    }
于 2021-08-04T18:58:56.580 回答
0

您的代码仅计算每行输入中单词的第一次出现。您需要遍历每个输入字符串以查找所有事件。尝试这样的事情:

int main(int argc,char *argv[])
{
    char buf[1026] = "start";
    int len, matches = 0;

    if (argc < 2) {
        printf("Please specify a program argument.");
        exit(1);
    }
    len = strlen(argv[1]);
    while (strncmp(buf,"#EOF",4) != 0) {
        fgets(buf,1025,stdin);
        int buflen = strlen(buf);
        for (int i = 0; i <= buflen - len; ++i) {
            if (strncmp(&buf[i],argv[1],len) == 0)
                ++matches;
        }
    }
    printf("'%s' found %d times\n",argv[1],matches);
    return 0;
}
于 2021-08-04T19:14:46.637 回答
0

这是根据Fred Larson给出的答案编写的功能正确的代码

非常感谢他。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char buf[1026]="start";
    int N;
    int count=0;
    char *p;

    if(argc==1)
    {
        printf("Please specify a program argument.\n");
        return(1);
    }

    N=strlen(argv[1]);

    while (strncmp(buf,"#EOF",4)!=0)
    {
        fgets(buf, 1025, stdin);
        for (p = buf;*p;p++)
        {
            if (strncmp(p, argv[1], N)==0)
            {
                if (strncmp(buf,"#EOF",4)!=0)
                    count++;
            }
        }
    }

    if(argc>=2)
        printf("%d\n", count);


    return 0;
}
于 2021-08-08T16:16:20.627 回答