我想编写一个代码来计算参数在输入中出现的频率。这些是要求:
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 时没有输入
提前致谢