首页 > 解决方案 > 函数没有通过某些测试用例

问题描述

我对代码战挑战解决方案的一项测试有疑问。我必须编写一个函数来返回输入字符串中字符的字母位置。我的解决方案如下。我通过了所有测试以及来自 codewars 的测试,但在这个测试中失败了(我没有实现这个测试代码,它是由 codewars 实现的测试代码的一部分):

Test(number_tests, should_pass) {
    srand(time(NULL));
    char in[11] = {0};
    char *ptr;
    for (int i = 0; i < 15; i++) {
      for (int j = 0; j < 10; j++) {
        char c = rand() % 10;
        in[j] = c + '0';
      }
      ptr = alphabet_position(in);
      cr_assert_eq(strcmp(ptr, ""), 0);
      free(ptr); 
    }
}

我收到的错误如下:表达式(strcmp(ptr, "")) == (0) is false.感谢您的帮助!ps 另外我注意到我正在泄漏内存(我不知道如何解决这个问题,所以我想我会使用数组来跟踪字符串而不使用 malloc)--> 我想这不是我会的问题在 main 函数中只是 free(ptr) 。

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

char *alphabet_position(char *text);

// test 
int main()
{
    if (!strcmp("1 2 3", alphabet_position("abc")))
    {
        printf("success...\n");
    }
    else
    {
        printf("fail...\n");
    }

    if (!strcmp("", alphabet_position("..")))
    {
        printf("success...\n");
    }
    else
    {
        printf("fail...\n");
    }
    if (!strcmp("20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11", alphabet_position("The sunset sets at twelve o' clock.")))
    {
        printf("success...\n");
    }
    else
    {
        printf("fail...\n");
    }


}

char *alphabet_position(char *text) 
{

  // signature: string -> string
  // purpose: extact alphabet position of letters in input string and
  // return string of alphabet positions

  // return "123"; // stub

  // track numerical value of each letter according to it's alphabet position 
  char *alph = "abcdefghijklmnopqrstuvwxyz";
  // allocate maximum possible space for return string
  // each char maps to two digit number + trailing space after number
  char *s = malloc(sizeof(char) * (3 * strlen(text) + 1));
  // keep track of the begining of return string
  char *head = s;

  int index = 0;
  int flag = 0;

  while(*text != '\0')
  {
      if ( ((*text > 64) && (*text < 91)) || ((*text > 96) && (*text < 123)))
      {
          flag = 1;
          index = (int)(strchr(alph, tolower(*text)) - alph) + 1;
          if (index > 9)
          {
            int n = index / 10;
            int m = index % 10;
            *s = n + '0';
            s++;
            *s = m + '0';
            s++;
            *s = ' ';
            s++;

          }
          else
          {
            *s = index + '0';
            s++;
            *s = ' ';
            s++;
          }
      }
    text++;
  }
  if (flag != 0)  // if string contains at least one letter
  {
  *(s -1) = '\0'; // remove the trailing space and insert string termination
  }
  return head;
}

标签: cstring

解决方案


这是我认为正在发生的事情:

在输入字符串中没有一个字符是字母字符的情况下从不s使用,因此分配的内存malloc()可以是任何东西。 malloc()不清除/归零内存。

您输入的".."通行证案例只是巧合。codewars 测试用例连续做了很多这样的非字母测试,每一个都会导致 a malloc(),如果其中任何一个失败,整个事情就会失败。

我试图重现这种情况,但它(正如我所说)是不可预测的。为了测试这一点,添加一个调试行来输出swhen flagis still的值0

if (flag != 0) { // if string contains at least one letter
    *(s -1) = '\0'; // remove the trailing space and insert string termination
}
else {
    printf("flag is still 0 : %s\n", s);
}

我敢打赌,有时你会得到一个不是"".


推荐阅读