无法使用strcmp

标签 c

所以我试图构建一个 coe,它使用一个字符串来检查字谜词,每次在原始字符串中出现一个字母时,该字符串就会加一。

我通过将两个字符串与 strcmp 进行比较来做到这一点,但出于某种原因,我总是知道字符串是相等的,即使它们不相等,我真的很感激一些反馈。

谢谢!

int firstWord[] = { 0 }, secondWord[] = { 0 }, len, i, j, anagrams=0;
char string[1000];

gets_s(string, sizeof(string));
len = strlen(string);
i = 0;
while (i < len) {
    if (string[i] == ' ') {
        for (j = i + 1; j < len; j++) {
            secondWord[string[j]]++;
        }
    }
    if (string[i] != ' ')
        firstWord[string[i]]++;
    if (strcmp(firstWord, secondWord) == 0) {
        anagrams++;
    }
    i++;
}

最佳答案

您的数组 firstWordsecondWord 各自只有一个 int 容量。数组不会像代码假设的那样动态调整大小。

int firstWord[] = { 0 }

这段代码的意思是创建一个数组,其中恰好有一个元素,其值为0

以下几行:

firstWord[string[i]]++;
secondWord[string[j]]++;

都会损坏随机内存并且根本不会扩展数组。

关于无法使用strcmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44118594/

上一篇:c - 解析txt文件C

下一篇:C 转换说明符

相关文章:

c# - 如何以编程方式清除 Windows 下的回收站?

c - 为什么 scanf 需要 &?

ios - 知道所有回调都已使用 libevent 和 bufferevent_free 运行

c - 如何在 c 中查找程序的执行时间和内存使用情况

c - 通过结构将整个数组分配给 C 中的另一个数组

c - 吉文 : c compiler cannot create executables - tidalcycles installation

c - Makefile: "ld: can' t 与主可执行文件链接”在 C

c - gcc 不会编译和运行 MySQL C 库

c++ - 用C/C++程序获取串口ARM CPU信息

c - 如何检查内存是否可用于读/写操作?