编辑代码

using System;

public class HelloWorld
{
 // 蛮力字符串匹配
    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 = 0;
            while (j < m && text[i + j] == pattern[j])
            {
                j++;
            }

            if (j == m)
            {
                // 匹配成功,返回起始位置
                return i;
            }
        }

        // 未找到匹配,返回 -1
        return -1;
    }

    static void Judge(string text, string pattern)
    {
        int result = BruteForceStringMatch(text, pattern);

        if (result != -1)
        {
            Console.WriteLine($"字符串匹配成功,起始位置:{result}");
        }
        else
        {
            Console.WriteLine("未找到匹配");
        }
    }

    static void Main()
    {
        string text = "abcddeffgabccbacome";
        string pattern = "abccba";
        Judge(text, pattern);

        text = "iloveyou";
        pattern = "loveu";
        Judge(text, pattern);

        text = " ";
        pattern = "";
        Judge(text, pattern);
    }
}