Wednesday, 18 September 2013

Crashing C Program

Crashing C Program

I am trying to write an algorithm that searches a document for a
particular chunk. If the chunk is found, a 1 will be returned, if not, a
0. Basically if the chunk is "abcd" and a particular length is 2, the
chunk would be split into "ab" and "cd." Then search the document to see
if either "ab" or "cd" is there. I think my algorithm is pretty much fine,
but the program keeps crashing. I think it has to do with the strncmp, but
I can't figure out how to fix it.
Here's my code:
int main( )
{
char s1[] = "abcdef";
char s3[] = "cd";
size_t s1Size = strlen(s1);
int k = 2;
if(simple_substr_match(s3,k,s1,s1Size))
printf("Match Found\n");
else
printf("No Match Found\n");
return 0;
}
int simple_substr_match(const unsigned char *ps, int k, const unsigned
char *ts, int n)
{
int isMatch;
int i;
int j;
for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
{
if( (strncmp(ts[i],ps[j], k)) == 0)
{
isMatch = 1;
break;
}
}
ps+=k;
}
return isMatch;
}

No comments:

Post a Comment