编辑代码

public class StringMatching {

    public static int bruteForceStringMatch(String text, String pattern) {
        int n = text.length();
        int m = pattern.length();

        for (int i = 0; i <= n - m; i++) {
            int j;
            for (j = 0; j < m; j++) {
                if (text.charAt(i + j) != pattern.charAt(j)) {
                    break;
                }
            }
            if (j == m) {
                return i; // 匹配成功,返回位置
            }
        }

        return -1; // 匹配失败
    }

    public static void main(String[] args) {
        String text = "Hello, world!";
        String pattern = "world";
        int startIndex = bruteForceStringMatch(text, pattern);
        
        if (startIndex != -1) {
            System.out.println("Pattern found at index " + startIndex);
        } else {
            System.out.println("Pattern not found");
        }
    }
}