Прервано (сброшено ядро) при поиске наивного шаблона подстроки в C - PullRequest
0 голосов
/ 20 октября 2019

Я пытаюсь написать наивный поиск паттернов на языке Си. В основном перебирайте каждую возможную подстроку и сравнивайте. Но возвращаемый индекс всегда равен 0. И появляется ошибка Aborted(core dumped).

Вот файлы:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>

#include "rkgrep.h"

Моя функция:

int
naive_substring_match(const char *pattern, const char *doc, int *first_match_ind)
{
    const char *p1,*p2,*p3;
    int i=0,j=0,flag=0;
    p1= doc;
    p2=pattern;
    for(i = 0; i<strlen(doc); i++)
    {
        if(*p1 == *p2)
         {
            p3 = p1;
            for(j = 0;j<strlen(pattern);j++)
            {
                if(*p3 == *p2)
                {
                  p3++;p2++;
                }
                else
                break;
            }
            p2 = pattern;
            if(j == strlen(pattern))
            {
                flag = 1;
                return i;
            }
        }
        p1++;
    }
    if(flag==0)
   {
       return -1;
   }
}
...