Pattern Matching

Code Id 27
Date Updated 3/7/2010
Title Pattern matching  
Description
This program searches and matches a particular pattern form a given string/sentence. 
                                  
Codes Snippet
#include
#include
main()
{
char text[25],pattern[80];
int position;
puts(�Enter the text :�);
gets(text);
put(�Enter the pattern for matching�);
gets(pattern);
position=pmatch(text,pattern);
if(position = = 0)
        printf(�%s does not exist in %sn�, text, pattern);
else
printf(�%s starts at %dn�, text, position);
}/*End of main()*/
int pmatch(char text[], char pattern[]) 
{       int matched, i, j, k = 0;
if(strlen(pattern) > strlen(text))
        return 0;
for(i=0; i<=strlen(text); i++)
{       k=0;
        for(j=k; k<=strlen(pattern); j++,k++)
        {
                if(pattern[k]!=text[j])
                {       matched = 0;
                        break;
                }
                else
                        matched = 1;
        }/*End of for*/
        if (matched == 1)
                return (i+1);
}/*End of for*/
return 0;
}/*End of pmatch*/

Comments are closed.