strstr() find the substring index into the larger string and returns the index of first occurrence. Below is the code implementation in java. However note that strstr() equivalent in java is indexOf() which do the same thing.
public static int strStr(String large, String possibleSubStr){ for(int i = 0; i < large.length(); i++ ) { for(int j = 0; j < possibleSubStr.length() && i+j < large.length(); j++ ) { if(possibleSubStr.charAt(j) != large.charAt(i+j)) { break; } else if (j == possibleSubStr.length()-1) { return i; } } } return -1; } public static void main (String args[]){ String large = "abcde"; String small = "cd"; System.out.println(strStr(large, small)); }
0 comments:
Post a Comment