C语言 如何在字符串算法中添加计数器来删除重复的单词?

vulvrdjw  于 2023-04-05  发布在  其他
关注(0)|答案(2)|浏览(88)

我有一个算法可以删除字符串中的重复单词:

int main()
{
    char str[100], word[100], doubleArr[10][30];
    int i = 0, j = 0, k = 0, len1 = 0, len2 = 0, l = 0, n, c = 0;

    printf("Enter the string\n");
    gets(str);
    n = strlen(str);

    for (i = 0; str[i] != '\0'; i++) {
        if (str[i] == ' ') {
            doubleArr[k][j] = '\0';
            k++;
            j = 0;
        } else {
            doubleArr[k][j] = str[i];
            j++;
        }
    }

    doubleArr[k][j] = '\0';

    j = 0;
    for (i = 0; i < k; i++) {
        for (l = 1; l < k + 1; l++) {
            if (doubleArr[l][j] == '\0' || l == i) {
                continue;
            }

            if (strcmp(doubleArr[i], doubleArr[l]) == 0) {
                doubleArr[l][j] = '\0';
            }
        }
    }

    j = 0;

    for (i = 0; i < k + 1; i++) {
        if (doubleArr[i][j] == '\0') {
            continue;
        } else {
            printf("%s ", doubleArr[i]);
        }
    }
}

我需要做一个字符串中重复单词的计数器。
当我试图把计数器放在代码的不同地方时,总是得到错误的结果。

lymnna71

lymnna711#

要对重复项进行计数,只需在测试if (strcmp(doubleArr[i], doubleArr[l]) == 0)之后,在检测到重复项时递增计数器
但是,请注意,您的代码中存在许多小问题:

  • 永远不要使用gets()
  • 你应该只增加字计数k当当前字有至少1个字符.
  • 如果句子不以空格结尾,则单词计数可能不正确:如果j > 0,则在循环结束后递增它,并在代码的其余部分使用k而不是k + 1
  • 如果任何字长于29个字符,或者由于缓冲区溢出而存在多于10个字,则代码具有未定义行为。

以下是修改后的版本:

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

int main(void) {
    char str[100], words[50][100];
    int i, j, k, n, dup;

    // input the string with fgets
    printf("Enter the string\n");
    if (!fgets(str, sizeof str, stdin))
        return 1;

    // extract the words from the string
    j = n = 0;
    for (i = 0; str[i] != '\0'; i++) {
        // use space and newline as word separators
        if (str[i] == ' ' || str[i] == '\n') {
            if (j > 0) {
                // add a new word if not empty
                words[n][j] = '\0';
                n++;
                j = 0;
            }
        } else {
            words[n][j] = str[i];
            j++;
        }
    }

    // add the last word if not empty
    if (j > 0) {
        words[n][j] = '\0';
        n++;
    }

    // remove duplicates
    dup = 0;
    k = 0;
    for (i = 0; i < n; i++) {
        for (j = 0; j < k; j++) {
            if (strcmp(words[i], words[j]) == 0)
                break;
        }
        if (j == k) {
            // keep this word
            if (i != k) {
                strcpy(words[k], words[i]);
            }
            k++;
        } else {
            // omit this word
            dup++;
        }
    }
    // update word count
    n = k;

    // output the word list
    for (i = 0; i < n; i++) {
        printf("%s%c", words[i], " \n"[i == n - 1]);
    }
    printf("%d duplicates\n", dup);
    return 0;
}
uidvcgyl

uidvcgyl2#

创建一个全局变量int counter = 0;
所以第32行if语句应该如下所示:

if (strcmp (doubleArr[i], doubleArr[l]) == 0) {
            doubleArr[l][j] = '\0';
            counter += 1;
    }

然后你可以打印它在任何你想要的

printf("%d", counter);

pls upvote if correct:)

相关问题