-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaive.java
More file actions
40 lines (35 loc) · 1.13 KB
/
Naive.java
File metadata and controls
40 lines (35 loc) · 1.13 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
package Algorithms;
import java.util.*;
public class Naive
{
public static void Naivesearch(String pattern, String givenstr)
{
int m = pattern.length();
int n = givenstr.length();
int flag=0,i=0;
for (i = 0; i <= n - m; i++)
{
int j;
//For current index i, check for pattern match
for (j = 0; j < m; j++)
if (givenstr.charAt(i + j) != pattern.charAt(j))
break;
if (j == m) {// if pattern found in the given string
flag=1;
}
}
if(flag==1)
System.out.println("Pattern found");
else
System.out.println("Pattern not found");
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string: ");
String givenstr=sc.next();
System.out.println("Enter the Pattern to be matched: ");
String pattern = sc.next();
Naivesearch(pattern,givenstr);
}
}