比较C中的两个字符数组。

时间:2021-12-20 12:48:50

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]) ?

如果(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;
    }

5 个解决方案

#1


6  

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()之类的内置函数,这可能是通过编写或生成优化的汇编代码来提高性能。如果字符串是相等的和0 (false),那么are_equal()函数将返回1 (true)。

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]);
}

The inline function assumes a C99 compiler; you can replace it with an appropriate macro if you're stuck using C89.

内联函数采用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->c and s2->c are null-terminated and that s1->length == strlen(s1->c) and s2->length == strlen(s2->c).

两个版本的代码都假设s1->c和s2->c中的字符串是null终止的,而s1->长度== strlen(s1->c)和s2->长度== strlen(s2->c)。

With C99, it would also be possible to use _Bool as the return type, or <stdbool.h> and bool (as the return type) and true and false as the return values.

使用C99,也可以使用_Bool作为返回类型,或者 和bool(作为返回类型),true和false作为返回值。 。h>

Alternative solution using 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否则,

int are_equal(const struct string *s1, const struct string *s2)
{
    return strcmp(s1->c, s2->c) == 0;
}

#2


8  

Assuming you can use C-strings with \0 termination I would do that and use strcmp:

假设你可以使用C-strings和\0终止,我将这样做并使用strcmp:

if (strcmp(s1.c, s2.c)) {
    // action if strings are not equal
}

#3


3  

Your if statement is incomplete (perhaps lacking setting a flag then a break or a return), and you don't use the struct so perhaps

您的if语句是不完整的(可能没有设置标志,然后是中断或返回),因此可能不使用struct。

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 strncmp i.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;
}

#4


1  

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)) { 
  }

#5


1  

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 strncmp preferably 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] == '\0')
      return 0;
  return s1[i] - s2[i];
}

#1


6  

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()之类的内置函数,这可能是通过编写或生成优化的汇编代码来提高性能。如果字符串是相等的和0 (false),那么are_equal()函数将返回1 (true)。

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]);
}

The inline function assumes a C99 compiler; you can replace it with an appropriate macro if you're stuck using C89.

内联函数采用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->c and s2->c are null-terminated and that s1->length == strlen(s1->c) and s2->length == strlen(s2->c).

两个版本的代码都假设s1->c和s2->c中的字符串是null终止的,而s1->长度== strlen(s1->c)和s2->长度== strlen(s2->c)。

With C99, it would also be possible to use _Bool as the return type, or <stdbool.h> and bool (as the return type) and true and false as the return values.

使用C99,也可以使用_Bool作为返回类型,或者 和bool(作为返回类型),true和false作为返回值。 。h>

Alternative solution using 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否则,

int are_equal(const struct string *s1, const struct string *s2)
{
    return strcmp(s1->c, s2->c) == 0;
}

#2


8  

Assuming you can use C-strings with \0 termination I would do that and use strcmp:

假设你可以使用C-strings和\0终止,我将这样做并使用strcmp:

if (strcmp(s1.c, s2.c)) {
    // action if strings are not equal
}

#3


3  

Your if statement is incomplete (perhaps lacking setting a flag then a break or a return), and you don't use the struct so perhaps

您的if语句是不完整的(可能没有设置标志,然后是中断或返回),因此可能不使用struct。

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 strncmp i.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;
}

#4


1  

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)) { 
  }

#5


1  

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 strncmp preferably 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] == '\0')
      return 0;
  return s1[i] - s2[i];
}