-
Notifications
You must be signed in to change notification settings - Fork 2
/
brute_force.c
executable file
·55 lines (52 loc) · 1.19 KB
/
brute_force.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
/*
Checks if char* q starts with char* p
*/
int strstarts(const char *p, const char *q)
{
while(*p && *p == *q){
++p;
++q;
}
// if *p is null return true else return diff
return *p ? *p - *q: 0;
}
/*
Checks at each point if the needle is substring of haystack
*/
int brute_force(const char *needle, const char *haystack)
{
const char *h = haystack;
for(;*h;++h){
if(strstarts(needle, h) == 0)
return h - haystack;
}
return -1;
}
int main(int argc, char **argv)
{
#if DEBUG
{
char needle[100];
char haystack[1000];
while(scanf("%s %s", needle, haystack) != -1){
int index = brute_force(needle, haystack);
if(index >= 0)
printf("Found %s in %s at %d\n", needle, haystack, index);
else
printf("Failed to find %s in %s\n", needle, haystack);
}
}
#elif
if(argc < 3){
printf("Usage : <command> <pattern> <string>\n");
return 0;
}
int index = brute_force(argv[1], argv[2]);
if(index >= 0)
printf("Found %s in %s at %d\n", argv[1], argv[2], index);
else
printf("Failed to find %s in %s\n", argv[1], argv[2]);
#endif
return 0;
}