im supposed to write a code for fast string matching for class, i wrote the function find_string, and it worked for random strings i tested on my own but the test code doesnt output the correct answer, can anyone help as to why its not working? thank you
Code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define POSITION 89765
using namespace std;
int length(char const*s){
int i = 0;
while(s[i] != '\0'){++i;}
return i;}
int find_string(char *s, char *t)
{
int x = length(s);
int y = length(t);
int i;
for(i=0;i<=(x-y);i++){
int j=0;
while((j<y) && (s[i+j] == t[j])){
j++;
}
if(j==y){return i;}
}
return -1;
}
int main(void)
{ char sequence[100001];
char pattern[1000];
int i, j;
for(i=0; i<100000; i++)
sequence[i] = 'a';
sequence[100000]='\0';
for(i=0; i<4000; i++)
{ j = rand()%100000;
sequence[j]='b';
}
for(j=0; j< 1000; j++)
pattern[j] = sequence[POSITION+j];
pattern[1000] = '\0';
if(find_string(sequence, pattern) == POSITION )
printf("accepted\n");
else
printf("needs check?\n");
}
Comment