首页 文章

C中的字符串比较函数有什么问题?如何比较字符串?

提问于
浏览
0

我昨天在接受采访时被要求编写一个函数来比较两个字符串,基本上与strcmp()的输出相同 . 我编写了以下程序和compare()函数,但被告知错了 . 采访者说,“你比较字符串从低字节到高字节 . 如果发生string1具有较小的较低字节但更高的字节,你的代码将输出string1小于字符串2,这是错误的 . ”

我想当我们进行字符串比较时,我们从左到右比较两个字符串,并将每对相应的字符与它们的ASCII值进行比较 . 我还找到了strcmp()的一些源代码,并尝试了很多案例来比较我的结果和strcmp()的结果,所以我认为该程序是正确的 .

我把我的程序写在这里的面试中 . 为了比较,我打印了我写的函数和strcmp()的值 . 我不得不说它不是很简洁 .

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compare (char *string1, char *string2, int length1, int length2);

int main()
{

 int len1,len2;
 char *str1;
 char *str2;
 int result;
 int result1;

//Input the string1
 printf("Please input the length of string1\n");
 scanf("%d", &len1);
 str1=malloc(sizeof(char)*len1);
 printf("Please input string 1:\n");
 scanf("%s",str1);

//Input the string2
 printf("Please input the length of string2\n");
 scanf("%d", &len2);
 str2=malloc(sizeof(char)*len2);
 printf("Please input string 2:\n");
 scanf("%s",str2);

//Do comparison, Both compare() and strcmp() are used
 result=compare(str1,str2,len1,len2);
 result1=strcmp(str1,str2);
 printf("\nThe result of compare() is: %d\n",result);
 printf("The result of strcmp() is:%d\n",result1);


 return 0;
}



int compare (char *string1, char *string2,int length1, int length2)
//If string1>string2, return1; if string1<string2, return -1; if string1=string2, return 0
{
 int result=0;

// Use the shorter length to do comprison bit by bit
 int length=(length1>length2)?length2:length1;


 for(int i=0;i<length-1;i++)
 {
  if(string1[i]>string2[i])
  {
   result=1;
   printf("%d\n",result);
   break;
  }
  else if (string1[i]<string2[i])
  {
   result=-1;
   printf("%d\n",result);
   break;
  }

 }


  if(result==1)
  {
   return 1;
  }
  else if (result==-1)
  {
   return -1;
  }
  else if (length1>length2)
  {
   return 1;
  }
  else if (length1<length2)
  {
   return -1;
  }
  else
  {
   return 0;
  }

}

所以有人能告诉我程序中有什么问题吗?你能给我一个例子,比较compare()和strcmp()的结果是不一样的吗?

谢谢!

1 回答

  • 1

    您传递错误的字符串长度或str1和str2的内存分配是错误的,导致scanf中的未定义行为 .

    例如,内存分配是:

    str2 = malloc(sizeof(char) * len2);
    

    那么只有 len2 个字符可以在数组 str2 (包括nul char)中,字符串长度可以是 len2 - 1 .

    我建议做一些事情(阅读评论):

    int max_lenght = 128;  // defined a constant  
    str1 = malloc(max_lenght);
    printf("Please input string 1:\n");
    fgets(str1, max_lenght, stdin);
    len1 = strlen(str1); // calculate length
    

    你不需要检查结果值,如果它等于 -1 则返回-1,如果为0则返回0 ...就像这样:

    int result=0, i;  // result is 0 
    for(i=0; i < length-1; i++)
    {
      if(string1[i] > string2[i])
      {
           result = 1; // result is 1
           break;
      }
      else if (string1[i] < string2[i])
      {
           result = -1; // result -1
           break;
      }
    }
    return result;  // return what is result is 
      // comparison like if(result == -1) return -1 not needed
    

    实际上更简单如下:

    for(result=0, i=0; i < length-1; i++){
       if(string1[i] == string2[i])
           continue;  // just continue until equal 
       if(string1[i] > string2[i])
           result = 1;
       else
           result = -1;
       break;  // else break 
    }
    return result;
    

相关问题