我的代码计算给定字符串上的关键字数量,但我需要将重复的关键字计为 1 并将其计为唯一关键字。有人可以帮我吗?我不知道该怎么做:(.please?
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "\nauto else else do while >> \nfor 1+2 == goto";
char *keywords[] =
{ "auto", "break", "else", "case", "if" };
int s = 0, i, x = 1;
char *result;
char delims[] = " \n";
int keyword = 0, ukeyword = 0;
printf("Input: %s", string);
//Count the number of lines
for (i = 0; string[i] != '\0'; ++i)
{
if (string[i] == '\n')
++s;
}
printf("\n\nNo. of lines: %d", s);
result = strtok(string, delims);
while (result != NULL )
{
for (x = 0; x != 5; x++)
{
if (strcmp(result, keywords[x]) == 0)
{
keyword++;
}
}
result = strtok(NULL, delims);
}
printf("\nNo. of keywords: %d.\n", keyword);
printf("\nNo. of unique keywords: %d.\n", ukeyword);
return 0;
}
所以输出应该是:
Input:
auto else else do while >>
for 1+2 == goto
No. of lines:2
No. of keywords:3
No. of unique keywords:2