Skip to content

Commit 99985d2

Browse files
committed
checks for output string in given input string
1 parent f520c2e commit 99985d2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Basic programs/StringMatching.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package BasicPractice;
2+
3+
public class StringMatching {
4+
5+
public static void main(String[] args) {
6+
7+
String input = "abcdyesefg";
8+
String output = "yes";
9+
10+
System.out.println(searchString(input, output)); // 4
11+
System.out.println(searchString(input, "yeb")); // -1
12+
}
13+
14+
public static int searchString(String input, String output) {
15+
16+
int inplen = input.length();
17+
int oplen = output.length();
18+
19+
if (inplen == 0 | oplen == 0 | oplen > inplen) {
20+
return -1;
21+
}
22+
23+
for (int index = 0; index < inplen - oplen; index++) {
24+
String possibleStr = input.substring(index, index + oplen);
25+
if (possibleStr.equals(output)) {
26+
return index;
27+
}
28+
}
29+
30+
return -1;
31+
}
32+
}

0 commit comments

Comments
 (0)