我有一个字符串结构。
struct string
{
char *c;
int length;
int maxLength;
}
我想检查两个字符串是否相等。
所以我想运行一个for循环。
for(int i = 0; i < length; i++)
if(s1[i] != s2[i]) // This code is more C# than C.
s1 和 s2 都是字符串结构。
我该怎么做 if(s1[i] != s2[i])
?
编辑: 我刚刚做了这个,是不是杀完了?
for(i = 0; i < length; i++)
if((*s1).c[i] != (*s2).c[i])
{
printf("Failed");
return 0;
}
最佳答案
假设您可以使用带有 \0
终止符的 C 字符串,我会这样做并使用 strcmp :
if (strcmp(s1.c, s2.c)) {
// action if strings are not equal
}
关于比较 C 中的两个字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18886637/