class StringMatch { public static int stringMatch(String str, String line) { int n = str.length(), m = line.length(); if (n == 0) // str is empty return(0); for (int i = 0 ; i < m ; i++) { if ( n > m - i ) return (-1); if (line.charAt(i) != str.charAt(0)) // first chars different continue; // skip rest of loop body // compare remaining characters // until first mismatch or end of string int j, k; for ( j = i+1, k = 1; k < n && line.charAt(j)==str.charAt(k) ; k++, j++) { } if (k == n) // end of str is reached return(j-n); // successful match } return(-1); // failed to match } public static void main(String[] args) { int k; if ( args.length == 2 ) if ( (k = stringMatch(args[0], args[1])) > -1 ) System.out.println("matched at " + k); else System.out.println("no match"); } }