写一个函数,输入父字符串和子字符串,返回这个子字符串在父字符串中首次出现的位置,如:ABCDEFGH 子串,EF 结果:5注意父串和字串的是可以任意输入的,越界问题暂不考虑!高手帮我!

解决方案 »

  1.   

    这个很简单!
                string fStr = "ABCDEFGH";
                string sStr = "EF";
                int index = fStr.IndexOf(sStr);
      

  2.   


                Console.WriteLine("请输入父字符串;");
                string fStr = Console.ReadLine();            Console.WriteLine("请输入子字符串:");
                string cStr = Console.ReadLine();            int index = fStr.IndexOf(cStr);            Console.WriteLine("字符串"+cStr+"在父字符串"+fStr+"中首次出现的位置是:"+index.ToString());
                Console.ReadKey();
      

  3.   

    int index = fStr.IndexOf(cStr);老大,不让使用字符串函数啊,要自己写,帮帮我吧
      

  4.   

    IndexOf获取字符串位置
    不用函数遍历
      

  5.   

    KMP算法即可。详见数据结构书本。
      

  6.   

    根据三楼的加工
    没测试,大概思路就是这样子
    在父串从开头的字符开始依次取子串长度的字符串出来
    进行比较,如果相同,就确定了位置
               Console.WriteLine("请输入父字符串;");
                string fStr = Console.ReadLine();            Console.WriteLine("请输入子字符串:");
                string cStr = Console.ReadLine();            int index = -1;   //-1表示未找到
               for (int i=0;i<=fStr.Length-cStr.Length;i++)
               {
                    if(fStr.Substring(i,cStr.Length)==cStr)
                    {
                         index=i;
                         break;
                     }            }            Console.WriteLine("字符串"+cStr+"在父字符串"+fStr+"中首次出现的位置是:"+index.ToString());
                Console.ReadKey();