public int search(CharSequence text, String pattern) {
        int M = text.length();
        int N = pattern.length();

        for (int i = 0; i < M - N; i++) {
            int j;
            for (j = 0; j < N; i++)
                if (text.charAt(i + j) != pattern.charAt(j))
                    break;
            if (j == N) return i;
        }
        return N;
    }