/* filename: pattern.java author: Chee Yap date: Nov 15, 2000 Notes: for honors basic algorithms class */ class pattern { // firstIndex(t,p) returns the index of p in t. // It returns -1 if p does not occur in t public static int firstIndex(String t, String p) { if (p.length() == 0) return 0; int d = t.length() - p.length(); if (d < 0) return -1; for (int i = 0; i < d; i++) { int j = 0; while (j< p.length() && (t.charAt(i+j) == p.charAt(j)) ) j++; if (j == p.length()) return i; } return -1; }//firstIndex public static void main (String[] args) { String p = "abaca"; String t = "abacbabacaab"; int ind = firstIndex(t,p); if (ind < 0) System.out.println("pattern not found"); else System.out.println("Pattern found at index " + ind); }//main }//class simple