forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Naive_String_Matching.java
44 lines (37 loc) · 1.03 KB
/
Naive_String_Matching.java
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
import java.util.Scanner;
class Naive_String_Matching
{
public static void search(String text, String pattern)
{
int lengthText = text.length();
int lengthPattern = pattern.length();
for(int i = 0; i <= lengthText - lengthPattern; i++)
{
int j;
for(j = 0; j < lengthPattern; j++)
if(text.charAt(i + j) != pattern.charAt(j))
break;
if(j == lengthPattern)
System.out.println("Pattern found at " + (i + 1));
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter text");
String text = sc.nextLine();
System.out.println("Enter pattern");
String pattern = sc.nextLine();
Naive_String_Matching obj = new Naive_String_Matching();
obj.search(text, pattern);
}
}
/*
Input:
text = "namanchamanbomanamansanam"
pattern = "aman"
Output:
Pattern found at 2
Pattern found at 8
Pattern found at 17
*/