-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaiveStringMatching.java
54 lines (44 loc) · 1019 Bytes
/
naiveStringMatching.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
45
46
47
48
49
50
51
52
53
54
import java.util.Scanner;
public class naiveStringMatching{
String text = "";
String pattern = "";
int tLen,pLen,count=0;
void inputData()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter Text String: ");
text = sc.next();
System.out.print("Enter Pattern String: ");
pattern = sc.next();
tLen = text.length();
pLen = pattern.length();
}
void searchForMatch()
{
for(int i=0 ; i<=(tLen-pLen) ; i++)
{
//Declare outside for block because required after for block to check
int j;
for(j=0;j<pLen;j++)
{
if(text.charAt(i+j) != pattern.charAt(j))
break;
}
//If all characters of the pattern found a match
if(j == pLen)
{
System.out.printf("\nPattern found at index %d",i);
count++;
}
}
//Message if no match found
if(count == 0)
System.out.println("NO MATCH FOUND");
}
public static void main(String args[])
{
naiveStringMatching obj = new naiveStringMatching();
obj.inputData();
obj.searchForMatch();
}
}