Pattern matching

This is a discussion on Pattern matching within the C Programming forums, part of the General Programming Boards category; 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]=='

Thread: Pattern matching

') { printf("match\n"); ...

  1. #1
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    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;
    }
    Last edited by manofsteel972; 10-08-2004 at 06:37 PM. Reason: oops sloppy formatting let me clean that up
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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?
    Last edited by manofsteel972; 10-08-2004 at 06:58 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Your optimized code works great. Finds the first match no problem. It should be easy to modify my code so it finds all matches.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. filename pattern matching
    By dwks in forum C Programming
    Replies: 11
    Last Post: 07-13-2005, 10:23 AM
  3. Pattern matching
    By GSLR in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-06-2003, 06:17 AM
  4. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  5. pattern matching
    By ahahplz in forum C Programming
    Replies: 5
    Last Post: 02-07-2003, 06:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21