Naive string matching algorithm in c
On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
code in C:#include<stdio.h>
#include<string.h>
void naive_string_matcher(char text[],char pat[])
{
char temp[100];
int n=strlen(text);
int m=strlen(pat);
int i,j,s,k,count=0;
for(s=0;s<=n;s++)
{
for(j=s,k=0;j<m;j++,k++)
temp[k]=text[s+k];
temp[k]='\0';
if(strcmp(pat,temp)==0)
{
printf("\n Pattern Occurs With Shift : %d ",s);
count++;
}
m++;
}
printf("\n\n No Of Times Pattern Occurs %d:",count);
}
int main()
{
char text[100],pat[100];
printf("\n ENTER THE TEXT : ");
gets(text);
printf("\n ENTER THE PATTERN : ");
gets(pat);
naive_string_matcher(text,pat);
return 0;
}
output:-ENTER THE TEXT : Welcome To CampusCoke ENTER THE PATTERN : C Pattern Occurs With Shift : 11 Pattern Occurs With Shift : 17 No Of Times Pattern Occurs 2: --------------------------------

0 comments: