Java examples for Data Structure:Search
Search pattern in a string, return all offset index
class Main/*ww w .j ava 2 s . c o m*/ { public static void search(String text, String pattern) { int lengthText = text.length(); int lengthPattern = pattern.length(); for(int i = 0; i <= lengthText - lengthPattern; i++) { int j; for(j = 0; j < lengthPattern; j++) if(text.charAt(i + j) != pattern.charAt(j)) break; if(j == lengthPattern) System.out.println("Pattern found at " + (i + 1)); } } public static void main(String[] args) { String text = "namanchamanbomanamansanam"; String pattern = "aman"; search(text, pattern); } }