【问题标题】:Substring algo with Haystack and Needle issue带有 Haystack 和 Needle 问题的子串算法
【发布时间】:2021-03-25 00:10:31
【问题描述】:
//created library file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int myStrStr(char *haystack, char *needle, char *buffer) {
    int i = 0;
    int k = 0;
    int data1 = strlen(haystack); 
    int data2 = strlen(needle);   
    int j = 0;

    for (i = 0; i <= data1 - data2; i++) {
        for (j = 0; j < data2; j++) {
            if (haystack[i+j] != needle[j]) {
                break;
            } else {
                buffer[k] = haystack[i+j];
                buffer[k+1] = '\0';
                k++;
            }   
        }
        if (j == data2) {
            {           
                return 1;
            }
        }
    }
    return 0; 
}   
//driver code
#include "mystrstr.h"
#include "mystrstr.h"
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main() {
  {
    printf("\t-----------------------------------\n");
    printf("\t-   Part A: String Manipulation   -\n");
    printf("\t-----------------------------------\n");

    char haystack[][20] = { "chocolate", "vanilla", "caramel", "strawberry", "banana", "cherry" };
    char needle[][20] = { "choc", "lla", "am", "strawberry", "na", "terrible" };
    char buffer[255];

    printf("\n\t=========Testing myStrStr with '%s' and substring '%s'===========\n\n", haystack[0], needle[0]);
    int result = myStrStr(haystack[0], needle[0], buffer);
    assert(result == 1 && strcmp(needle[0], buffer) == 0);
    printf("\n\t\t....Test Passed\n");

    printf("\n\t=========Testing myStrStr with '%s' and substring '%s'===========\n\n", haystack[1], needle[1]);
    result = myStrStr(haystack[1], needle[1], buffer);
    printf("needle: %s, buffer: %s\n", needle[1], buffer);
    assert(result == 1 && strcmp(needle[1], buffer) == 0);
    printf("\n\t\t....Test Passed\n");

    printf("\n\t=========Testing myStrStr with 'blueberry' and substring 'ueber'===========\n\n");
    result = myStrStr(haystack[2], needle[2], buffer);
    assert(result == 1 && strcmp(needle[2], buffer) == 0);
    printf("\n\t\t....Test Passed\n");

    printf("\n\t=========Testing myStrStr with 'strawberry' and substring 'strawberry'===========\n\n");
    result = myStrStr(haystack[3], needle[3], buffer);
    assert(result == 1 && strcmp(needle[3], buffer) == 0);
    printf("\n\t\t....Test Passed\n");

    printf("\n\t=========Testing myStrStr with 'banana' and substring 'na'===========\n\n");
    result = myStrStr(haystack[4], needle[4], buffer);
    assert(result == 1 && strcmp(needle[4], buffer) == 0);
    printf("\n\t\t....Test Passed\n");

    printf("\n\t=========Testing myStrStr with 'grapefruit' and substring 'terrible'===========\n\n");
    result = myStrStr(haystack[5], needle[5], buffer);
    assert(result == 0);
    printf("\n\t\t....Test Passed\n");
    }
//////////////////////////////////////////////////////////////////////////////
/*    {
    printf("\t-----------------------------------\n");
    printf("\t-   Part B: Pointer Arithmetic   -\n");
    printf("\t-----------------------------------\n");
    char haystack[][20] = { "chocolate", "vanilla", "caramel", "strawberry", "banana", "cherry" };
    char needle[][20] = { "choc", "lla", "am", "strawberry", "na", "terrible" };
    char buffer[255];
    printf("\n\t=========Testing myStrStr with '%s' and substring '%s'===========\n\n", haystack[0], needle[0]);
    int result = myStrStrPA(haystack[0], needle[0], buffer);
    assert(result == 1 && strcmp(needle[0], buffer) == 0);
    printf("\n\t\t....Test Passed\n");
    printf("\n\t=========Testing myStrStr with '%s' and substring '%s'===========\n\n", haystack[1], needle[1]);
    result = myStrStrPA(haystack[1], needle[1], buffer);
    printf("needle: %s, buffer: %s\n", needle[1], buffer );
    assert(result == 1 && strcmp(needle[1], buffer) == 0);
    printf("\n\t\t....Test Passed\n");
    printf("\n\t=========Testing myStrStr with 'blueberry' and substring 'ueber'===========\n\n");
    result = myStrStrPA(haystack[2], needle[2], buffer);
    assert(result == 1 && strcmp(needle[2], buffer) == 0);
    printf("\n\t\t....Test Passed\n");
    printf("\n\t=========Testing myStrStr with 'strawberry' and substring 'strawberry'===========\n\n");
    result = myStrStrPA(haystack[3], needle[3], buffer);
    assert(result == 1 && strcmp(needle[3], buffer) == 0);
    printf("\n\t\t....Test Passed\n");
    printf("\n\t=========Testing myStrStr with 'banana' and substring 'na'===========\n\n");
    result = myStrStrPA(haystack[4], needle[4], buffer);
    assert(result == 1 && strcmp(needle[4], buffer) == 0);
    printf("\n\t\t....Test Passed\n");
    printf("\n\t=========Testing myStrStr with 'grapefruit' and substring 'terrible'===========\n\n");
    result = myStrStrPA(haystack[5], needle[5], buffer);
    assert(result == 0);
    printf("\n\t\t....Test Passed\n");
    printf("\n\t=========All Tests Passed. Don't forget to run with valgrind and submit on mycourses!===========\n\n");
    } */
    return 0;
}
//header file
#ifndef JG_MYSTRSTR_H
#define JG_MYSTRSTR_H

int myStrStr(char *haystack, char *needle, char *buffer);

#endif

在这里运行我的代码时,我试图让我的函数通过所有测试,但是当它到达 A 部分的第三次测试时,它失败了。我收到错误“a.out: main.c:35: main: Assertion `result == 1 && strcmp(needle[2], buffer) == 0' failed. Aborted' failed. Aborted”。我不明白为什么我的代码没有通过驱动程序代码中的第三次测试。

【问题讨论】:

  • 在调试器中运行您的代码并逐行执行。之后,如果您仍然需要帮助,请将代码缩减为 minimal reproducible example。例如,删除除失败的测试用例之外的所有测试用例。
  • printf("%s\n%s\n", needle[2], buffer);给出“am”和“aam”,所以你的测试当然失败了。

标签: arrays c string


【解决方案1】:

发现不匹配后忘记重置k

        if(haystack[i+j] != needle[j]){
            k = 0; /* add this */
            break;
        }

【讨论】:

  • 也测试不是'blueberry'和子串'ueber'而是'caramel'和'am'
  • 在内部循环之前将 k 初始化为 0 似乎更好。
猜你喜欢
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
相关资源
最近更新 更多