C program to Implement brute-force method of string matching

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 int i,j,k,n,m,flag=0;
 char t[40],p[30];
 clrscr();
 printf("Enter text: ");
 gets(t);
 printf("\nEnter pattern: ");
 gets(p);
 n=strlen(t);
 m=strlen(p);
 for(i=0;i<=n-m;i++)
 {
  j=0;
  while(j<m && p[j]==t[j+i])
   {
     j++;
     if(j==m)
     {
      flag=1;
      k=i+1;
     }
     else
      flag=0;
   }
 }
 if(flag==1)
    printf("\nPattern found at position: %d\n ",k);
 else
    printf("\nPattern not found in text \n");
  getch();
}

Output:


3 comments:

  1. No comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:
    Post a CommentNo comments:

    ReplyDelete