1
0
Fork 0
beispiele_pr2/src/main/java/pr2/ads/textsearch/BruteForceTextSearch.java

23 lines
556 B
Java

package pr2.algorithmen.textsearch;
public class BruteForceTextSearch implements TextSearch {
public int search(String haystack, String needle) {
int n = haystack.length();
int m = needle.length();
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
break;
}
}
if (j == m) {
return i;
}
}
return NOT_FOUND;
}
}