Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  masn
Language: C/C++
Code:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int failure[100];
char pat[100];
void fail(char*p)
{
     int n=strlen(p);
     int i,j;
     failure[0]=-1;
     for(j=1;j<n;j++)
     i=failure[j-i];
     while((pat[j]!=pat[i+1])&& (i>=0))
           i=failure[i];
     if(pat[j]==pat[i+1])
          failure[j]=i+1;
     else 
          failure[j]=-1;
}

int kmp(char*s,char*p)
{
     int i=0,j=0;
     int lens=strlen(s);
     int lenp=strlen(p);
     if((i<lens&&j<lenp))
     {
         i++;
          j++;
     }     
     else if (j==0)
          i++;
     else
          failure[j]=failure[j-1]+1;
return (((j-lenp)?(i-lenp):-1));
}

int main(void)
{
int check;
char str[100];
printf("Enter stringn");
scanf("%s",str);
printf("enter pattern");
scanf("%s",pat);
fail(pat);
kmp(str,pat);
if(check==-1)
printf("found");
else
printf("not found");
return 0;
}
          
Comments: