在C中不使用strcmp来比较Char数组

时间:2022-11-17 21:19:42

Im looking for a way to compare 2 char arrays without strcmp. Is this the way to go? Or am I missing something? WHen I compiled it, if I type in the same strings in both, the program gets stuck and wont do anything. PLEASE HELP!

我正在寻找一种比较2个没有strcmp的char数组的方法。这是要走的路吗?或者我错过了什么?当我编译它时,如果我在两者中键入相同的字符串,程序就会卡住并且不会做任何事情。请帮忙!

EDIT: SORRY IT WAS MEANT TO BE A i not a C

编辑:抱歉,这是一个我不是C

int compare_info(char *array1, char *array2)
{

int i;
i = 0;

while(array1[i] == array2[i])
{
    if(array1[i] == '\0' || array2[i] == '\0')
        break;
    i++;
}

if(array1[i] == '\0' && array2[i] == '\0')
return 0;

else
    return-1;

}

6 个解决方案

#1


3  

Here you have a solution, is prety like your code, but I have made some changes. I took out the returns in the middle of the loop, because they break the structure, in this way it is easier to analyze. Finishing, I added a new condition to the while, so when the end of string is found, the loop ends

在这里你有一个解决方案,像你的代码一样,但我做了一些改变。我在循环中间取出了返回,因为它们破坏了结构,这样就更容易分析了。整理,我添加了一个新的条件,所以当找到字符串的结尾时,循环结束

int compare_info(char *array1, char *array2)
{
    int i;
    int response = 0;
    i = 0;

    while(array1[i] == array2[i] && response == 0 )
    {
        if(array1[i] == '\0' || array2[i] == '\0'){
            response = 1;
        }
        i++;
    }

    return response;
}

#2


3  

Here you have a solution

在这里你有一个解决方案

int compare_info(char *array1, char *array2)
{
    if ((array1 == NULL) || (array2 == NULL))
        return 0;
    while ((*array1 != '\0') && (*array2 != '\0') && (*array1 == *array2))
    {
        array1++;
        array2++;
    }
    return (*array1 == *array2);
}

or perhaps you like this more

或许你更喜欢这个

int compare_info(char *array1, char *array2)
{
    int i;

    i = 0;
    if ((array1 == NULL) || (array2 == NULL))
        return 0;
    while ((array1[i] != '\0') && (array2[i] != '\0') && (array1[i] == array2[i]))
    {
        i++;
    }
    return (array1[i] == array2[i]);
}

you can make the arguments const it would be better style since if you only want to compare the strings, you guarantee that the function wont modify them i mean compare_info(const char *array1, const char *array2)

你可以使参数const成为更好的样式,因为如果你只想比较字符串,你保证函数不会修改它们我的意思是compare_info(const char * array1,const char * array2)

#3


2  

In addition to the other answers, I would add the following to the mix as a strcmp look-alike without strcmp:

除了其他答案之外,我会在没有strcmp的情况下将以下内容添加到混合中作为strcmp外观相似:

int strcmp_nohdr (char *s1, char *s2)
{
    if (!s1 && !s2) return 0;
    if (s1 && !s2) return 1;
    if (!s1 && s2) return -1;

    while (*s1 && *s2 && *s1 == *s2) s1++, s2++;

    if (!*s1 && !*s2) return 0;
    else if (*s1 > *s2) return 1;
    else return -1;
}

examples/output:

$ strcmp_nohdr mydog mycat

 s1 is greater than s2

$ strcmp_nohdr mybat mycat

 s1 is less than s2

$ strcmp_nohdr mycat mycat

 s1 is equal to s2

$ strcmp_nohdr mycat myca

 s1 is greater than s2

$ strcmp_nohdr myca mycat

 s1 is less than s2

A further alternative would be to call and assembly routine that does the same thing.

另一个替代方案是调用和组装例程,它执行相同的操作。

#4


1  

Unlike some people, I tend to prefer catching null pointers as early as possible (via program crash and a debugger), so I avoid any checks for null pointers below. In other words, don't pass a null pointer to these functions.

与某些人不同,我倾向于尽早捕获空指针(通过程序崩溃和调试器),所以我避免在下面检查空指针。换句话说,不要将空指针传递给这些函数。

The str_neq function determines whether the strings are not equal, which seems to be the correct logic based upon your code (return 0/false if equal and -1/true if not equal):

str_neq函数确定字符串是否相等,这似乎是基于您的代码的正确逻辑(如果相等则返回0 / false,如果不相等则返回-1 / true):

int
str_neq (const char *s1, const char *s2)
{
  while (*s1 != '\0' && *s1 == *s2)
    ++s1, ++s2;
  return -(*s1 != *s2);
}

To provide the same behavior as strcmp requires a small change in the expression that computes the return value:

要提供与strcmp相同的行为,需要对计算返回值的表达式进行少量更改:

int
str_compare (const char *s1, const char *s2)
{
  while (*s1 != '\0' && *s1 == *s2)
    ++s1, ++s2;
  return *s1 - *s2;
}

I hope this helps! :-)

我希望这有帮助! :-)

#5


0  

Shortly:

int compare_info(char * array1, char * array2)
{
    char * ptr1 = array1;
    char * ptr2 = array2;

    while(*ptr1 && *ptr2 && *ptr1++==*ptr2++);
    if(!*ptr1 && !*ptr2 && *(ptr1-1)==*(ptr2-1)) return 0;
    return -1;
}

When two arrays is same return 0, else return -1. (It's not equal with strcmp.)

当两个数组相同时返回0,否则返回-1。 (它与strcmp不相同。)

#6


0  

The following function should mimic the exact behavior of strcmp:

以下函数应该模仿strcmp的确切行为:

int compare_info(const char* array1,const char* array2)
{
    int i;
    for (i=0; array1[i]!=0 && array2[i]!=0; i++)
    {
        if (array1[i] > array2[i])
            return +1;
        if (array1[i] < array2[i])
            return -1;
    }
    if (array1[i] != 0)
        return +1;
    if (array2[i] != 0)
        return -1;
    return 0;
}

#1


3  

Here you have a solution, is prety like your code, but I have made some changes. I took out the returns in the middle of the loop, because they break the structure, in this way it is easier to analyze. Finishing, I added a new condition to the while, so when the end of string is found, the loop ends

在这里你有一个解决方案,像你的代码一样,但我做了一些改变。我在循环中间取出了返回,因为它们破坏了结构,这样就更容易分析了。整理,我添加了一个新的条件,所以当找到字符串的结尾时,循环结束

int compare_info(char *array1, char *array2)
{
    int i;
    int response = 0;
    i = 0;

    while(array1[i] == array2[i] && response == 0 )
    {
        if(array1[i] == '\0' || array2[i] == '\0'){
            response = 1;
        }
        i++;
    }

    return response;
}

#2


3  

Here you have a solution

在这里你有一个解决方案

int compare_info(char *array1, char *array2)
{
    if ((array1 == NULL) || (array2 == NULL))
        return 0;
    while ((*array1 != '\0') && (*array2 != '\0') && (*array1 == *array2))
    {
        array1++;
        array2++;
    }
    return (*array1 == *array2);
}

or perhaps you like this more

或许你更喜欢这个

int compare_info(char *array1, char *array2)
{
    int i;

    i = 0;
    if ((array1 == NULL) || (array2 == NULL))
        return 0;
    while ((array1[i] != '\0') && (array2[i] != '\0') && (array1[i] == array2[i]))
    {
        i++;
    }
    return (array1[i] == array2[i]);
}

you can make the arguments const it would be better style since if you only want to compare the strings, you guarantee that the function wont modify them i mean compare_info(const char *array1, const char *array2)

你可以使参数const成为更好的样式,因为如果你只想比较字符串,你保证函数不会修改它们我的意思是compare_info(const char * array1,const char * array2)

#3


2  

In addition to the other answers, I would add the following to the mix as a strcmp look-alike without strcmp:

除了其他答案之外,我会在没有strcmp的情况下将以下内容添加到混合中作为strcmp外观相似:

int strcmp_nohdr (char *s1, char *s2)
{
    if (!s1 && !s2) return 0;
    if (s1 && !s2) return 1;
    if (!s1 && s2) return -1;

    while (*s1 && *s2 && *s1 == *s2) s1++, s2++;

    if (!*s1 && !*s2) return 0;
    else if (*s1 > *s2) return 1;
    else return -1;
}

examples/output:

$ strcmp_nohdr mydog mycat

 s1 is greater than s2

$ strcmp_nohdr mybat mycat

 s1 is less than s2

$ strcmp_nohdr mycat mycat

 s1 is equal to s2

$ strcmp_nohdr mycat myca

 s1 is greater than s2

$ strcmp_nohdr myca mycat

 s1 is less than s2

A further alternative would be to call and assembly routine that does the same thing.

另一个替代方案是调用和组装例程,它执行相同的操作。

#4


1  

Unlike some people, I tend to prefer catching null pointers as early as possible (via program crash and a debugger), so I avoid any checks for null pointers below. In other words, don't pass a null pointer to these functions.

与某些人不同,我倾向于尽早捕获空指针(通过程序崩溃和调试器),所以我避免在下面检查空指针。换句话说,不要将空指针传递给这些函数。

The str_neq function determines whether the strings are not equal, which seems to be the correct logic based upon your code (return 0/false if equal and -1/true if not equal):

str_neq函数确定字符串是否相等,这似乎是基于您的代码的正确逻辑(如果相等则返回0 / false,如果不相等则返回-1 / true):

int
str_neq (const char *s1, const char *s2)
{
  while (*s1 != '\0' && *s1 == *s2)
    ++s1, ++s2;
  return -(*s1 != *s2);
}

To provide the same behavior as strcmp requires a small change in the expression that computes the return value:

要提供与strcmp相同的行为,需要对计算返回值的表达式进行少量更改:

int
str_compare (const char *s1, const char *s2)
{
  while (*s1 != '\0' && *s1 == *s2)
    ++s1, ++s2;
  return *s1 - *s2;
}

I hope this helps! :-)

我希望这有帮助! :-)

#5


0  

Shortly:

int compare_info(char * array1, char * array2)
{
    char * ptr1 = array1;
    char * ptr2 = array2;

    while(*ptr1 && *ptr2 && *ptr1++==*ptr2++);
    if(!*ptr1 && !*ptr2 && *(ptr1-1)==*(ptr2-1)) return 0;
    return -1;
}

When two arrays is same return 0, else return -1. (It's not equal with strcmp.)

当两个数组相同时返回0,否则返回-1。 (它与strcmp不相同。)

#6


0  

The following function should mimic the exact behavior of strcmp:

以下函数应该模仿strcmp的确切行为:

int compare_info(const char* array1,const char* array2)
{
    int i;
    for (i=0; array1[i]!=0 && array2[i]!=0; i++)
    {
        if (array1[i] > array2[i])
            return +1;
        if (array1[i] < array2[i])
            return -1;
    }
    if (array1[i] != 0)
        return +1;
    if (array2[i] != 0)
        return -1;
    return 0;
}