-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteForceStringMatch.java
More file actions
43 lines (38 loc) · 1.05 KB
/
Copy pathBruteForceStringMatch.java
File metadata and controls
43 lines (38 loc) · 1.05 KB
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
import java.util.Scanner;
public class BruteForceStringMatch
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String text, pattern;
int n, m;
int foundIndex = -1;
System.out.println("Enter the Text String:");
text = sc.nextLine();
System.out.println("Enter the Pattern String:");
pattern = sc.nextLine();
n = text.length();
m = pattern.length();
for (int i = 0; i <= n - m; i++)
{
int c = 0, a = i;
for (int j = 0; j < m; j++, a++)
{
if (text.charAt(a) != pattern.charAt(j))
{
c = 1;
break;
}
}
if (c == 0)
{
foundIndex = i;
break;
}
}
if (foundIndex != -1)
System.out.println("Pattern found at position: " + (foundIndex + 1));
else
System.out.println("Pattern not found");
}
}