【问题标题】:My String compare isn't working in Cpp我的字符串比较在 Cpp 中不起作用
【发布时间】:2013-11-11 17:38:53
【问题描述】:
// Compares the two arguments.  If they are equal, 0 is returned.  If they
// are not equal, the difference of the first unequal pair of characters
// is returned.
int strcmp(const char* charPointerToArray1, const char* charPointerToArray2) {
    int i = 0;
    // we need to check if both arrays have reached their terminating character
    // because consider the case where array1 = { '\0' } and array2 = { '1', '\0' }
    while (charPointerToArray1[i] != '\0' || charPointerToArray2[i] != '\0') {
        // while iterating through both char arrays,
        // if 1 char difference is found, the 2 char arrays are not equal
        if (charPointerToArray1[i] != charPointerToArray2[i]) {
            int differenceOfFirstUnequalChars = charPointerToArray1[i] - charPointerToArray2[i];
            return differenceOfFirstUnequalChars;
        } else {
            i++;
        }
    }
    return 0; // charPointerToArray1 == charPointerToArray2
}

于是我在cpp中写了一个字符串比较的方法,搞不明白是什么问题。

【问题讨论】:

  • || 应该是 &&。然后你需要稍微重写一下逻辑。在您在该行上方评论中的非常示例中,您将超出if 条件的范围。
  • 我正要说明错误,但我敢打赌 Simple 拥有它的权利。
  • 另外,如果两个字符串的长度不同?
  • 在什么输入中它不起作用?任何或特殊情况?是否存在误报、误报或两者兼有?
  • 当我在 2 个字符串 '\0' 和 'foobar\0' 上运行该方法时,它返回 0 而不是 f 和 '\0' 之间的差异

标签: c++ string


【解决方案1】:

只要其他人正在显示代码,这就是我对此的看法。没有必要不断地将第一个和第二个字符与 0 进行比较,然后再相互比较。 一旦两个字符之一为 0,您就完成了,您可以返回差值rr 不需要被初始化,因为while 测试的第二部分总是被执行——它是一个,所以这两个部分都需要为真。


另外值得注意的是:我看到我本能地反转了结果的符号。在比较字符串 A 和 B 时,您可能想知道“A 是否小于比 B”,这将由否定结果表示。

#include <stdio.h>

int my_strcmp (const char* charPointerToArray1, const char* charPointerToArray2)
{
    int i = 0, r;
    while ((charPointerToArray1[i] || charPointerToArray2[i]) &&
        !(r = (charPointerToArray2[i] - charPointerToArray1[i])))
    {
        i++;
    }
    return r;
}

int main (void)
{
    printf("%d\n", my_strcmp("foobar", ""));
    printf("%d\n", my_strcmp("foobar", "foobaz"));
    printf("%d\n", my_strcmp("foobar", "foobar"));
    return 0;
}

【讨论】:

    【解决方案2】:

    这是工作示例:

    // Compares the two arguments.  If they are equal, 0 is returned.  If they
    // are not equal, the difference of the first unequal pair of characters
    // is returned.
    int strcmp(const char* charPointerToArray1, const char* charPointerToArray2) {
        int i = 0;
        // we need to check if both arrays have reached their terminating character
        // because consider the case where array1 = { '\0' } and array2 = { '1', '\0' }
        while (charPointerToArray1[i] != '\0' && charPointerToArray2[i] != '\0') {
            // while iterating through both char arrays,
            // if 1 char difference is found, the 2 char arrays are not equal
            if (charPointerToArray1[i] != charPointerToArray2[i]) {
                int differenceOfFirstUnequalChars = charPointerToArray1[i] - charPointerToArray2[i];
                return differenceOfFirstUnequalChars;
            } else {
                i++;
            }
        }
        // return 0; // not really, one of the array may be longer than the other
        if (charPointerToArray1[i] == '\0' && charPointerToArray2[i] == '\0')
            return 0;
        else //... one of the array is longer
    }
    

    【讨论】:

      【解决方案3】:

      据我所知,您的功能很好。特别是,它确实适用于您说它不起作用的示例:

      #include <stdio.h>
      
      int my_strcmp(const char* charPointerToArray1, const char* charPointerToArray2) {
          int i = 0;
          // we need to check if both arrays have reached their terminating character
          // because consider the case where array1 = { '\0' } and array2 = { '1', '\0' }
          while (charPointerToArray1[i] != '\0' || charPointerToArray2[i] != '\0') {
              // while iterating through both char arrays,
              // if 1 char difference is found, the 2 char arrays are not equal
              if (charPointerToArray1[i] != charPointerToArray2[i]) {
                  int differenceOfFirstUnequalChars = charPointerToArray1[i] - charPointerToArray2[i];
                  return differenceOfFirstUnequalChars;
              } else {
                  i++;
              }
          }
          return 0; // charPointerToArray1 == charPointerToArray2
      }
      
      
      int main() {
        printf("%d\n", my_strcmp("", "foobar"));
      }
      

      这会按预期打印一个负数。

      (我已重命名函数,以免混淆 strcmp() 被调用。我建议您也这样做。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-20
        • 2015-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-17
        相关资源
        最近更新 更多