函数返回另一个函数中的不同值

发布于 2025-02-13 15:18:29 字数 1470 浏览 3 评论 0原文

我的第一个功能检查了子字符串中包含其字符串的字母数量。

int num_matches(char* word, char* letters) {
    char scrabble[128];
    strcpy(scrabble, letters);
    int num = 0;

    for (int i = 0; i < strlen(word); i++) {
        if (strchr(letters, word[i]) == NULL) {
            return -1;
        }

        for (int j = 0; j < strlen(letters); j++) {
            if (word[i] == letters[j]) {
                num++;
                scrabble[j] = '\0';
                break;
            }
        }

    }
    return num;
}

它为“ QWOP”,“ QWOP”返回4。但是,在以下函数内部,即使我的调试尝试中的“ qwop”,“ qwop”,“ qwop”,即使在buff and Letters打印为“ qwop”时,它也会返回相同的不正确值。

void read_words(int num_count[128], char* (*word_idx)[128], int argc, char** argv) {
    FILE* fp = fopen(argv[1], "r");
    char* letters = argv[2];

    int idx = 0;
    char buff[128];
    int result = 0;
    while (fgets(buff, 128, fp) != NULL) {
        printf("buff:%s letters:%s\n", buff, letters);
        result = num_matches(buff, letters);
        printf("result: %d\n", result);
        num_count[idx] = result;
        char* word = malloc(strlen(buff) + 1);
        strcpy(word, buff);
        (*word_idx)[idx] = word;
        idx++;
        result = 0;
    }

    fclose(fp);
}
buff:QWOP
 letters:QWOP
result: -1

我的txt文件:

ABC
DEFG
QWOP
QWOP
QUOKKA
QUOLL
QUASH
QUANDONG

由于fgets停在新线上,并且文本文件中没有空格,所以我认为阅读buff不应该有任何问题。

My first function checks for the number of letters that a substring contains from its string.

int num_matches(char* word, char* letters) {
    char scrabble[128];
    strcpy(scrabble, letters);
    int num = 0;

    for (int i = 0; i < strlen(word); i++) {
        if (strchr(letters, word[i]) == NULL) {
            return -1;
        }

        for (int j = 0; j < strlen(letters); j++) {
            if (word[i] == letters[j]) {
                num++;
                scrabble[j] = '\0';
                break;
            }
        }

    }
    return num;
}

It returns 4 for "QWOP", "QWOP". However, inside the following function it is returning the same incorrect value for every function call, even when buff and letters print as "QWOP", "QWOP" from my debugging attempt.

void read_words(int num_count[128], char* (*word_idx)[128], int argc, char** argv) {
    FILE* fp = fopen(argv[1], "r");
    char* letters = argv[2];

    int idx = 0;
    char buff[128];
    int result = 0;
    while (fgets(buff, 128, fp) != NULL) {
        printf("buff:%s letters:%s\n", buff, letters);
        result = num_matches(buff, letters);
        printf("result: %d\n", result);
        num_count[idx] = result;
        char* word = malloc(strlen(buff) + 1);
        strcpy(word, buff);
        (*word_idx)[idx] = word;
        idx++;
        result = 0;
    }

    fclose(fp);
}
buff:QWOP
 letters:QWOP
result: -1

My txt file:

ABC
DEFG
QWOP
QWOP
QUOKKA
QUOLL
QUASH
QUANDONG

Since fgets stops at a newline and there are no spaces in my text file, I don't think there should be any problems with reading buff.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2025-02-20 15:18:29

fgets最后用\ n字符读取数据。

在这种情况下,您的功能将失败。它非常容易测试:

int main(void)
{
    printf("%d\n", num_matches("ABCD", "ABCD"));
    printf("%d\n", num_matches("ABCD\n", "ABCD"));
}

结果:

4
-1

您需要从buff中删除\ n

另一个问题是第二个功能。参数char*(* Word_idx)[128]是指向128个指针的指针。我认为这不是您想要的。

fgets reads the data with \n character at the end.

Your function will fail in this case. It is very easy to test:

int main(void)
{
    printf("%d\n", num_matches("ABCD", "ABCD"));
    printf("%d\n", num_matches("ABCD\n", "ABCD"));
}

Result:

4
-1

You need to remove \n from the buff.

Another problem is the second function. The parameter char* (*word_idx)[128] is a pointer to an array of 128 pointers to char. I do not think that is something you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文