public static string Reverse(string str)
        {
            string[] s = new string[100];
            string ss= "";
            int begin = 0;
            int end = str.Length;
            //拆分字符串到数组
            for (int i = 0; i < str.Length;i++ )
            {
                s[i] = str.Substring(i,1);
            }
           //交换
            while (begin <= end)
            {
                string temp = "";
                temp = s[begin];
                s[begin] = s[end];
                s[end] = temp;
                begin++;
                end--;
            }
            //组合字符串
            "大家注意这里"
            // for (int i = 0; i <=str.Length; i++)
             //{
             //    ss += s[i];
             //}
             //为什么多循环一反而正确了呢?
             //下面的的确错了
            for (int i = 0; i <str.Length; i++)
             {
                 ss += s[i];
             }
             return ss;
        }
        

解决方案 »

  1.   

     ss += s[i];
     这这一行放一个breakpoint看不就得了。
      

  2.   

    如果把交换的代码注释掉,
    for (int i = 0; i <str.Length; i++)
                 {
                     ss += s[i];
                 }
    是可以的如果运行交换的代码部分for循环就要多运行一次才可以
    for (int i = 0; i <str.Length; i++)
                 {
                     ss += s[i];
                 }问题在这里,但是为什么会这样呢?请高手指点
      

  3.   

    下面的代码多了应该是
    for (int i = 0; i <=str.Length; i++)
                 {
                     ss += s[i];
                 }
      

  4.   

    为什么?
          while (begin <= end)
                {
                    string temp = "";
                    temp = s[begin];
                    s[begin] = s[end];
                    s[end] = temp;
                    begin++;
                    end--;
                }
    看看你这段语句,s[end]是什么? s[end]=null;所以你应该把begin<=end变为begin<=(end-1)
      

  5.   

    看错了,应该把end=str.Length;变为end=str.Length-1;
    因为最后一个元素的下标为str.Length-1.
    这下没错了
      

  6.   

    交换位置吧?
    char[] c = str.ToCharArray();
    str="";
    for (int i = 0; i <str.Length; i++)
     {
       str =str.Insert(0, c[i].ToString());
     }这样不就行了?
      

  7.   

    for (int i = 0; i <c.Length; i++) 
      

  8.   

    //拆分字符串到数组i要小于等于str.Length
                for (int i = 0; i < =str.Length;i++ )
                {
                    s[i] = str.Substring(i,1);
                }