-
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
-
- void Naive_Search_Algo(char *search_pattern,char *original_pattern
- {
- int m=strlen(search_pattern);
- int n=strlen(original_pattern);
- int i,j;
- for(i=0;i<n-m;i++)
- {
- for(j=0;j<m;j++)
- {
- if(original_pattern[i+j]!=search_pattern[j])
- break;
- if(j==m)
- printf("\n Searching String Is Founf At %d Location",i);
- }
- }
- }
-
- int main()
- {
- char search_pattern[100];
- char original_pattern[100];
- printf("\n Enter Original String");
- scanf("%s",original_string);
- printf("\n Enter Searching String");
- scanf("%s",Search_pattern);
- Naive_search_Algo(search_pattern,original_pattern);
- return 0;
- }
Algorithm of Naive pattern Search Algo
Searching for p in string t,
- for i from 0 to n-m :
- for j from 0 to m-1:
- if p[j]! = t[i+j]
- break out of inner loop
- if j==m:
- return i
- return-1
Worst - case complexity: Θ(mn), where m is the length of p and n is the length of t.