我試過這段代碼,但它似乎沒有工作,如何擺脫嵌套循環?函數返回1,如果兩個數組完全不同,如果有一個公共元素,則返回0
#include <stdio.h>
#include <string.h>
int meme(char s1[], char s2[])
{
int i = 0, j = 0;
int different;
while (i <= strlen(s1) && different == 1) {
while (j <= strlen(s2)) {
if (s1[i] != s2[j]) {
different = 1;
} else {
different = 0;
}
j = j + 1;
}
i = i + 1;
}
return different;
}
你是否嘗試用調試器逐步調試代碼? – pm100
一開始你沒有初始化不同 - 所以你的循環可能永遠不會運行 – pm100
'不同'是未定義的。順便說一句,爲你的用例使用'for'循環會更好 – RoiHatam