-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementStrstr.java
More file actions
36 lines (29 loc) · 833 Bytes
/
ImplementStrstr.java
File metadata and controls
36 lines (29 loc) · 833 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
package com.string;
public class ImplementStrstr {
public static void main(String[] args) {
// TODO Auto-generated method stub
String source ="";
String target ="";
System.out.println(strStr(source,target));
}
public static int strStr(String haystack, String needle) {
if (haystack == ""|| needle == ""){
return 0;
}
if (haystack == null || needle == null){
return -1;
}
int i,j;
for (i =0; i<haystack.length() - needle.length()+1; i++){
for (j =0; j< needle.length(); j++){
if (haystack.charAt(i+j) != needle.charAt(j)){
break;
}
}
if (j == needle.length()){
return i;
}
}
return -1;
}
}