-
Pattern matching
Code:
void testformatch(char pattern[],int pat_count,char buffer[],int buf_count)
{
while( pattern[pat_count] == buffer[buf_count] )
{
pat_count++;
buf_count++;
}
if(pattern[pat_count]=='\0')
{
printf("match\n");
pat_count=0;
}
else
{
pat_count=0;
}
}
I am trying to play with a simple algorithm to find a string for some reason Ican't get it to find it if they are exactly the same.
ie if pattern="ab2";
and buffer="ab2";
it won't give me a match.
Code:
int main(void){
char buffer[]="abc1";
char pattern[]="abc1";
int pat_count=0;
int buf_count=0;
while( (buffer[buf_count]) != '\0' )
{
testformatch(pattern,pat_count,buffer,buf_count);
buf_count++;
}
return 0;
}
-
You are incrementing past your null because a null is equal to a null. Thus, they're the same, and so you increment. Now you're beyond your loop, and have random crap (or a progarm crash), so they not longer test as equal.
Quzah.
-
ok fixed it with this Thanx!
Code:
void testformatch(char pattern[],int pat_count,char buffer[],int buf_count)
{
while( ( pattern[pat_count] == buffer[buf_count] )&&(buffer[buf_count]!='\0') )
{
pat_count++;
buf_count++;
}
if(pattern[pat_count]=='\0')
{
printf("match\n");
pat_count=0;
}
else
{
pat_count=0;
}
}
Any further suggestions? Optimizations?
-
You should check null for both. Otherwise the second could run out before the first.
Code:
while( a1[a1c] && a2[a2c] && a1[a1c++] == a2[a2c++] );
return a1[a1c] == a2[a2c];
Quzah.
-
Your optimized code works great. Finds the first match no problem. It should be easy to modify my code so it finds all matches.
-
Hm... Actually I think there's a bug in it:
Code:
while( a1[a1c] && a2[a2c] && a1[a1c++] == a2[a2c++] );
return a1[a1c] == a2[a2c];
"foo" "f0o"
Let's walk through it:
1) while f != null && f != null && f (increment to o) == f (increment to 0)
2) while o != null && O != null && o (increment to o) == O (increment to o) ... break because of the equality test
3) return o == o
Hm. Yep. That should return a pattern match. Probably then you'll want to seperate the incrementation from the actual check. Usually it's fine to do it that way, but that should be bugged, so you'll not want to use it there I believe.
I'm not at a compiler now, or I'd run a test case to be sure, but it looks like it should be wrong.
Quzah.
-
well I am sure there is a way to make it work I am just going to go with my original and optimize later after I have done everything I want to.
-
It'll work if you move incrementation past the check:
Code:
while( a1[a1c] && a2[a2c] && a1[a1c] == a2[a2c] );
{
a1c++;
a2c++;
}
return a1[a1c] == a2[a2c];
That way, if you walk through it as I did above, it won't increment on differences or null.
Quzah.
-
I tried it that way and it still only matched if they were exactly the same. I decided to remove pat_count from my param list as it is always 0 to start and just initialized it inside the function. I can see how your code works. i might play around with it later though. If I spend too much time decifering i won't spend enough actually programming. Lol