-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaiveStringMatching.java
More file actions
44 lines (33 loc) · 990 Bytes
/
NaiveStringMatching.java
File metadata and controls
44 lines (33 loc) · 990 Bytes
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
package com.mycompany.algorithm_final_project;
import java.util.Scanner;
/**
*
* @author israkkayumchowdhury
*/
public class NaiveStringMatching {
static int naiveStringMatching(String text, String pattern) {
int n = text.length();
int m = pattern.length();
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (text.charAt(i + j) != pattern.charAt(j)) {
break;
}
}
if (j == m) {
return i;
}
}
return -1;
}
public void main_func() {
Scanner s = new Scanner(System.in);
System.out.print(" Enter the text: ");
String text = s.nextLine();
System.out.print(" Enter the pattern: ");
String pattern = s.nextLine();
int index = naiveStringMatching(text, pattern);
System.out.println(" Pattern found at index: " + index);
}
}