File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <string.h>
3
+
4
+ /* Naive Pattern Search algorithm (brute force way) */
5
+ void naive_search (char * str , char * pattern )
6
+ {
7
+ int len_str = strlen (str );
8
+ int len_pat = strlen (pattern );
9
+
10
+ for (int i = 0 ; i <= len_str - len_pat ; i ++ )
11
+ {
12
+ int j ;
13
+ for (j = 0 ; j < len_pat ; j ++ )
14
+ {
15
+ if (str [i + j ] != pattern [j ])
16
+ break ;
17
+ }
18
+ if (j == len_pat )
19
+ printf ("--Pattern is found at: %d\n" , i );
20
+ }
21
+ }
22
+
23
+ int main ()
24
+ {
25
+ char str [] = "AABCAB12AFAABCABFFEGABCAB" ;
26
+ char pat1 [] = "ABCAB" ;
27
+ char pat2 [] = "FFF" ; /* not found */
28
+ char pat3 [] = "CAB" ;
29
+
30
+ printf ("String test: %s\n" , str );
31
+ printf ("Test1: search pattern %s\n" , pat1 );
32
+ naive_search (str , pat1 );
33
+ printf ("Test2: search pattern %s\n" , pat2 );
34
+ naive_search (str , pat2 );
35
+ printf ("Test3: search pattern %s\n" , pat3 );
36
+ naive_search (str , pat3 );
37
+ return 0 ;
38
+ }
You can’t perform that action at this time.
0 commit comments