C 数一数相同和重复的单词数

C 数一数相同和重复的单词数,c,C,我的代码计算给定字符串上的关键字数,但我需要将重复的关键字计算为1,并将其计算为唯一的关键字。有人能帮我吗?我不知道该怎么做:(.请 必须分别统计每个关键字的出现次数: int keyword_count[5]; 当您找到一个关键字时,您必须增加Correspondent计数器: keywords ++; keyword_count[x] ++; 最后,唯一的关键字是那些具有关键字计数[x]==1的关键字。向我们展示您的尝试。您需要一个集合/计数器数据结构,您必须实现它。先生,我尝试保存关键

我的代码计算给定字符串上的关键字数,但我需要将重复的关键字计算为1,并将其计算为唯一的关键字。有人能帮我吗?我不知道该怎么做:(.请


必须分别统计每个关键字的出现次数:

int keyword_count[5];
当您找到一个关键字时,您必须增加Correspondent计数器:

keywords ++;
keyword_count[x] ++;

最后,唯一的关键字是那些具有关键字计数[x]==1的关键字。向我们展示您的尝试。您需要一个集合/计数器数据结构,您必须实现它。先生,我尝试保存关键字(结果)要使用strcpy设置数组,但它不起作用,我的想法是将所有匹配的关键字保存到一个数组中,以便我可以跟踪已找到的单词。好的,先生,我将尝试实现此操作:)好的,先生,我还不允许投赞成票,但您的解决方案确实起了作用!:)我很高兴。
#include <stdio.h>
#include <string.h>

int totalKeywords, uniqueKeywords;

struct keywordStruct
{
    char *keyword;
    int count = 0;
} keywords[5];

void updateKeywordCount(char word[]) {
    int i = 0;
    for (; i < sizeof(keywords)/sizeof(keywordStruct); i++) {
        if (strcmp(keywords[i].keyword, word) == 0) {
            if (keywords[i].count == 0) {
                uniqueKeywords += 1;
                keywords[i].count = 1;
            }
            totalKeywords += 1;
            return;
        }
    }
}

int main(int argc, char const *argv[])
{
    char word[100], currentChar;
    int i, lines = 0, charInWordCount = 0;
    keywords[0].keyword = "auto";
    keywords[1].keyword = "break";
    keywords[2].keyword = "else";
    keywords[3].keyword = "cause";
    keywords[4].keyword = "if";
    freopen("Input.txt", "r", stdin);
    while (scanf("%c", &currentChar) == 1) {
        if (currentChar == '\n' || currentChar == '\r') {
            lines += 1;
            word[charInWordCount++] = '\0';
            charInWordCount = 0;
            updateKeywordCount(word);
        } else if (currentChar == ' ') {
            word[charInWordCount++] = '\0';
            charInWordCount = 0;
            updateKeywordCount(word);
        } else {
            word[charInWordCount++] = currentChar;
        }
    }
    if (charInWordCount) {
        lines += 1;
        word[charInWordCount++] = '\0';
        updateKeywordCount(word);
    }
    printf ("No. of lines: %d\n", lines);
    printf ("No. of keywords: %d\n", totalKeywords);
    printf ("No. of unique keywords: %d\n", uniqueKeywords);
    return 0;
}
keywords ++;
keyword_count[x] ++;
#include <stdio.h>
#include <string.h>

int totalKeywords, uniqueKeywords;

struct keywordStruct
{
    char *keyword;
    int count = 0;
} keywords[5];

void updateKeywordCount(char word[]) {
    int i = 0;
    for (; i < sizeof(keywords)/sizeof(keywordStruct); i++) {
        if (strcmp(keywords[i].keyword, word) == 0) {
            if (keywords[i].count == 0) {
                uniqueKeywords += 1;
                keywords[i].count = 1;
            }
            totalKeywords += 1;
            return;
        }
    }
}

int main(int argc, char const *argv[])
{
    char word[100], currentChar;
    int i, lines = 0, charInWordCount = 0;
    keywords[0].keyword = "auto";
    keywords[1].keyword = "break";
    keywords[2].keyword = "else";
    keywords[3].keyword = "cause";
    keywords[4].keyword = "if";
    freopen("Input.txt", "r", stdin);
    while (scanf("%c", &currentChar) == 1) {
        if (currentChar == '\n' || currentChar == '\r') {
            lines += 1;
            word[charInWordCount++] = '\0';
            charInWordCount = 0;
            updateKeywordCount(word);
        } else if (currentChar == ' ') {
            word[charInWordCount++] = '\0';
            charInWordCount = 0;
            updateKeywordCount(word);
        } else {
            word[charInWordCount++] = currentChar;
        }
    }
    if (charInWordCount) {
        lines += 1;
        word[charInWordCount++] = '\0';
        updateKeywordCount(word);
    }
    printf ("No. of lines: %d\n", lines);
    printf ("No. of keywords: %d\n", totalKeywords);
    printf ("No. of unique keywords: %d\n", uniqueKeywords);
    return 0;
}
No. of lines: 2
No. of keywords: 3
No. of unique keywords: 2