C语言 比较 C 中的两个字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18886637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare Two Character Arrays in C
提问by Mike John
I have a string struct.
我有一个字符串结构。
struct string
{
char *c;
int length;
int maxLength;
}
I want to check if two strings are equal.
我想检查两个字符串是否相等。
So I want to run a for loop.
所以我想运行一个for循环。
for(int i = 0; i < length; i++)
if(s1[i] != s2[i]) // This code is more C# than C.
s1 and s2 are both string structs.
s1 和 s2 都是字符串结构。
How do I do this if(s1[i] != s2[i])?
我该怎么做if(s1[i] != s2[i])?
EDIT:I just did this, is it over kill?
编辑:我刚刚做了这个,是不是已经结束了?
for(i = 0; i < length; i++)
if((*s1).c[i] != (*s2).c[i])
{
printf("Failed");
return 0;
}
回答by qwwqwwq
回答by Jonathan Leffler
I'm assuming you want to write the comparison code yourself, rather than using built-ins such as strcmp()— which might be performance boosted by being written in, or generated as, optimized assembler code. The are_equal()function will return 1 (true) if the strings are equal and 0 (false) otherwise.
我假设您想自己编写比较代码,而不是使用内置程序,例如strcmp()- 通过编写或生成为优化的汇编代码可能会提高性能。are_equal()如果字符串相等,该函数将返回 1(真),否则返回 0(假)。
Sub-optimal solution
次优解
static inline int min(int a, int b) { return (a < b) ? a : b; }
int are_equal(const struct string *s1, const struct string *s2)
{
int len = min(s1->length, s2->length);
int i;
for (i = 0; i < len; i++)
{
if (s1->c[i] != s2->c[i])
return 0; // They are different
}
return(s1->c[i] == s2->c[i]);
}
static inline int min(int a, int b) { return (a < b) ? a : b; }
int are_equal(const struct string *s1, const struct string *s2)
{
int len = min(s1->length, s2->length);
int i;
for (i = 0; i < len; i++)
{
if (s1->c[i] != s2->c[i])
return 0; // They are different
}
return(s1->c[i] == s2->c[i]);
}
The inlinefunction assumes a C99 compiler; you can replace it with an appropriate macro if you're stuck using C89.
该inline函数假定一个 C99 编译器;如果您坚持使用 C89,您可以用适当的宏替换它。
More nearly optimal solution
更接近最优解
int are_equal(const struct string *s1, const struct string *s2)
{
if (s1->length != s2->length)
return 0; // They must be different
for (int i = 0; i < s1->length; i++)
{
if (s1->c[i] != s2->c[i])
return 0; // They are different
}
return 1; // They must be the same
}
Both versions of the code assume that the strings in s1->cand s2->care null-terminated and that s1->length == strlen(s1->c)and s2->length == strlen(s2->c).
代码的两个版本假设在字符串s1->c和s2->c为空值终止,并且s1->length == strlen(s1->c)和s2->length == strlen(s2->c)。
With C99, it would also be possible to use _Boolas the return type, or <stdbool.h>and bool(as the return type) and trueand falseas the return values.
使用 C99,也可以使用_Bool作为返回类型,或<stdbool.h>和bool(作为返回类型)和true和false作为返回值。
Alternative solution using strcmp()
使用的替代解决方案 strcmp()
Note that if you simply use strcmp(), you will get 0 if the strings are equal and a non-zero value if the strings are unequal. So, you might also write the function like this to return true if the strings are equal and false otherwise:
请注意,如果您简单地使用strcmp(),如果字符串相等,您将获得 0,如果字符串不相等,您将获得非零值。因此,您也可以编写这样的函数,如果字符串相等则返回 true,否则返回 false:
int are_equal(const struct string *s1, const struct string *s2)
{
return strcmp(s1->c, s2->c) == 0;
}
回答by Basile Starynkevitch
Your ifstatement is incomplete (perhaps lacking setting a flag then a breakor a return), and you don't use the structso perhaps
您的if陈述不完整(可能缺少设置标志然后 abreak或 a return),并且您struct可能不使用so
struct string {
char *c;
int length;
int maxLength;
};
bool same_string (struct string *s1, struct string* s2) {
int ln1 = s1->length;
if (ln1 != s2->length) return false;
for (int i=0; i<ln1; i++)
if (s1->c[i] != s2[ci]) return false;
return true;
}
But you really want strncmpi.e. just
但你真的想要strncmp即只是
bool same_string (struct string *s1, struct string* s2) {
if (s1->length != s2->length) return false;
return strncmp(s1->c, s2->c, s1->length)==0;
}
回答by P0W
You need to compare each member
您需要比较每个成员
int compare(struct string s1, struct string s2){
return (strcmp(s1.c,s2.c) == 0) &&
(s1.maxLength ==s2.maxLength) &&
(s1.length ==s2.length) ;
}
for(int i = 0; i < length; i++)
if(!compare(s1,s2)) {
}
回答by Freddie
You don't really need to know the length of the strings to compare them. You can use the string compare tools in the standard library strncmppreferably over strcmp, or you can write your own similar to this:
你真的不需要知道字符串的长度来比较它们。您可以使用标准库中的字符串比较工具strncmp最好超过strcmp,或者您可以编写自己的类似于以下内容:
int strcmp(char *s1, char *s2)
{
int i;
for (i = 0; s1[i] == s2[i]; i++)
if (s1[i] == '##代码##')
return 0;
return s1[i] - s2[i];
}

