-
Notifications
You must be signed in to change notification settings - Fork 0
/
bruteforce.c
55 lines (46 loc) · 1.04 KB
/
bruteforce.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>
#include <string.h>
#include <stdbool.h>
#define MAX 20
int bruteForce(char *, char *);
bool main()
{
char string[MAX]; //= "atharmujtabawani";
char pattern[MAX]; //= "har";
printf("Enter the string: ");
scanf("%s", string);
printf("Enter the pattern to search: ");
scanf("%s", pattern);
if (bruteForce(string, pattern) == 0)
{
printf("pattern not found\n");
}
else
{
printf("pattern found at index %d\n", bruteForce(string, pattern));
}
return false;
}
int bruteForce(char string[], char pattern[])
{
int len_string = strlen(string);
int len_pattern = strlen(pattern);
int i, j;
int max = len_string - len_pattern + 1;
for (i = 0; i < max; i++)
{
bool flag = true;
for (j = 0; j < len_pattern && flag == true; j++)
{
if (pattern[j] != string[j + i])
{
flag = false;
}
}
if (flag == true)
{
return i;
}
}
return 0;
}