You are on page 1of 3

NAME:Jerio Soares ROLL:14 CLASS:SE-B/COMPS

SUBJECT:AOA EXPERIMENT:9

CODE:
#include<conio.h>
#include<stdio.h>
#include<math.h>

char T[30],P[10];
int d = 256,q = 11;
int T_length = 0,P_length = 0;
int t[20];
int m;
int sh_count = 0;

int compare(int s)
{
    int i;
    for(i=0;i<m;i++)
    {
        if(P[i] != T[s+i])
        break;
    }

    if(i == m)
    return 1;
    else
    return 0;
}

void Rabin_Karp(char T[],char P[],int d,int q)


{
    int i,s;
    int n = T_length;
    m = P_length;
    int h = pow(d,m-1);
    h = h % q;
    int p = 0;
    t[0] = 0;
    //printf("\nh:%d",h);

    for(i=0;i<m;i++)
NAME:Jerio Soares ROLL:14 CLASS:SE-B/COMPS
SUBJECT:AOA EXPERIMENT:9

    {
        p = ((d*p) + P[i] ) % q;
        t[0] = ((d*t[0]) + T[i] ) % q;
    }

    //printf("\np:%d",p);
    //printf("\nto:%d",t[0]);

    for(s=0;s<=n-m;s++)
    {
        //printf("\nt:%d",t[s]);
        if(p == t[s])
        {
            if(compare(s))
            printf("\nPattern Occurs with Shift %d",s);
            else
            sh_count++;
        }
       
        if(s < n-m)
        {
            t[s+1] = (d*(t[s]-T[s]*h) + T[s+m])%q;
        }

        if(t[s+1] < 0)
        t[s+1] = t[s+1] + q;
    }

void main()
{
    int i=0;
    printf("\nEnter the Sequence :");
    gets(T);
    printf("\nEnter the target Pattern :");
    gets(P);
    while(T[i] != '\0')
    {
        T_length++;
NAME:Jerio Soares ROLL:14 CLASS:SE-B/COMPS
SUBJECT:AOA EXPERIMENT:9

        i++;
    }
    i = 0;
    while(P[i] != '\0')
    {
        P_length++;
        i++;
    }

    Rabin_Karp(T,P,d,q);
    printf("\nThe number of Spurrious Hits are :%d",sh_count);
}
OUTPUT:

You might also like