用C#实现方法Welcome_to__Mircosoft!进行字符串反转,反转后的内容为:!Mircosoft__to_Welcome“_”代表空格,要求是不能使用任何现有的库函数,做一个方法返回值:string,参数:string

解决方案 »

  1.   

    唉,2个string,不断获取单个字符从后面,在添加进去不就是了吗?
      

  2.   

     char[] arr = original.ToCharArray();
     Array.Reverse(arr);
     return new string(arr);
      

  3.   

    http://hi.baidu.com/yuanwenxue/blog/item/8b8667275a350e09908f9de1.html
      

  4.   

    C#
    http://www.cnblogs.com/BpLoveGcy/articles/1692051.html
      

  5.   

    private static void TestRegex15()
    {
        string s = "Welcome_to__Mircosoft!";
        Regex regExp = new Regex(@"[a-zA-Z]+|[!]|_+", RegexOptions.Compiled);
        MatchCollection mc = regExp.Matches(s);
        int i = mc.Count;
        string result = regExp.Replace(s, delegate(Match m) { return mc[--i].Value; });
        Console.WriteLine(result);
    }
      

  6.   

     public string ReversalStr(string str)
            {
                string returnStr = "";
                if(!string.IsNullOrEmpty(str))
                {
                    int count = str.Length;
                    for(int index=count-1;index>=0 ;index--)
                    {
                        returnStr += str[index];
                        //returnStr+=
                    }
                }
                return returnStr;
            }
      

  7.   

    C# 中可以把string当成字符数组来处理,str[1]这样访问是允许的。
    1、用第一个字符和最后一个字符交换,注意临界。
    2、定义一个新串,将原串的字符倒着加起来。
    在算法和数据结构中常见这类问题。
      

  8.   

    for循环应该不是库函数吧没找到相关的东西。如果可以用for的话,正反两个循环就可以
      

  9.   


                    string[] sp=new string[] { "_","__","!"};
                    string[] ss = s.Split(sp, StringSplitOptions.RemoveEmptyEntries);
                    string s2 = "";
                    for (int i = ss.Length - 1; i >= 0; i--)
                    {
                        s2 +=  sp[i]+ss[i];
                    }
                    Response.Write(s2);//!Mircosoft__to_Welcome
    //这个很好玩,你可以试试
    string s = "Welcome_to__Mircosoft!";
                    string s1 = new string(s.ToCharArray().Reverse().ToArray());
                    Response.Write(s1); //!tfosocriM__ot_emocleW
      

  10.   

    不用现有库的话自己实现分割private static void TestRegex16()
    {
        string s = "Welcome_to__Mircosoft!";
        StringBuilder temp = new StringBuilder();
        List<string> sections = new List<string>();
        int status = -1;//-1 初始化 0 字母 1 符号 2 下划线
        foreach (char c in s)
        {
            int current_status = -1;
            if (char.IsLetter(c))
            {
                current_status = 0;
            }
            else if (c == '_')
            {
                current_status = 2;
            }
            else
            {
                current_status = 1;
            }
            if (current_status != status)
            {
                status = current_status;
                if (temp.Length > 0) sections.Insert(0, temp.ToString());
                temp.Remove(0, temp.Length);
            }
            temp.Append(c);
        }
        foreach (string section in sections)
        {
            temp.Append(section);
        }
        Console.WriteLine(temp.ToString());
    }输出:
    !Mircosoft__to_Welcome
      

  11.   


    public void ReturnStr(string str)
    {
       string tmp="",result="";
       for(int i = str.Length-1;i>-1;i--)
         {
            char a = str[i];
            if(i == str.Length-1)
             {
                if(a=='!')
                  {
                      result = a.ToString();
                  }
                if(a=='_')
                  {
                       result += tmp + '_';
                       tmp='_';
                   }
                   if(a=='!')
                     {
                        result+=tmp;
                        tmp="";
                     }
                     else{
                        tmp = str[i]+tmp
                        response.write(result);
                     }
             }
         }
    }
    我把整个串拆分成若干个char型,然后判断空格,再进行标点字符的判断上,我限制了代码的扩展
      

  12.   


    string s = "Welcome_to__Mircosoft!";
                    string[] sp=new string[] { "_","__","!"};
                    string[] ss = s.Split(sp, StringSplitOptions.RemoveEmptyEntries);
                    string s2 = "";
                    for (int i = ss.Length - 1; i >= 0; i--)
                    {
                        s2 +=  sp[i]+ss[i];
                    }
                    Response.Write(s2);//!Mircosoft__to_Welcome
    //这个很好玩,你可以试试
                    string s1 = new string(s.ToCharArray().Reverse().ToArray());
                    Response.Write(s1); //!tfosocriM__ot_emocleW
      

  13.   

    private static void TestRegex17()
    {
        string s = "Welcome_to__Mircosoft!";
        string temp = "";
        string result = "";
        int status = -1;//-1 初始化 0 字母 1 符号 2 下划线
        for (int i = 0; i < s.Length; i++)        
        {
            char c = s[i];
            int current_status = -1;
            if ((c >= 'a' && c <= 'z') | (c >= 'A' && c <= 'Z'))
            {
                current_status = 0;
            }
            else if (c == '_')
            {
                current_status = 2;
            }
            else
            {
                current_status = 1;
            }
            if (current_status != status)
            {
                status = current_status;
                if (temp.Length > 0) result = temp + result;
                temp = "";
            }
            temp += new string(c, 1);
        }
        result = temp + result;
        Console.WriteLine(result);
    }
    这下可以了?
      

  14.   


    这算是使用库函数了吧? Reverse()string returnString = "";
    char[] array = 字符串.ToCharArray();
    for(int i=array.length-1; i>=0; i--)
    {
       returnString += array[i]
    }return retrunString;
      

  15.   

            public void test()
            {
                string str = "Welcome_to__Mircosoft!";
                char[] ch1 = str.ToCharArray();
                str = "";
                for (int i = ch1.Length - 1; i >= 0; i--)
                {
                    str += ch1[i].ToString();
                }
            }
      

  16.   


    你不可用 Console.WriteLine(result); 输出
      

  17.   

    这个倒不用这么牵强,Console.WriteLine(result); 只是作为一个测试人员进行测试的代码。
    结贴了,28可用,我也做出一个新的方法,扩展性也不错,你们可以参考一下。string reverseString(string inStr)
            {
                string returnStr = "";
                string tmp = inStr[0].ToString();
                bool preChar = isChar(inStr[0]);
                for (int i = 1; i < inStr.Length; i++)
                {
                    char a = inStr[i];
                    bool b = isChar(a);
                    if (b)
                    {
                        if (preChar)
                            tmp += a.ToString();
                        else
                        {
                            returnStr = tmp + returnStr;
                            tmp = a.ToString();
                            if(i==inStr.Length-1)
                                returnStr = tmp + returnStr;
                        }
                    }
                    else
                    {
                        if (preChar)
                        {
                            returnStr = tmp + returnStr;
                            tmp = a.ToString();
                            if (i == inStr.Length - 1)
                                returnStr = tmp + returnStr;
                        }
                        else
                        {
                            tmp += a.ToString();
                        }
                    }
                    preChar = b;
                }
                return returnStr;
            }        bool isChar(char inChar)
            {
                if (((int)inChar >= 65 && (int)inChar <= 90) || ((int)inChar >= 97 && (int)inChar <= 122))
                    return true;
                else return false;
            }
      

  18.   

    原意大概是反转2次,第一次整个string反转Welcome_to__Mircosoft! =>
    !tfosocriM__ot_emocleW第二次,碰到_就反转前面的部分!tfosocriM__ot_emocleW =>
    !Mircosoft__to_Welcome这样空间是O(1)的
      

  19.   

    char* reverseString(char* pSource, int iInLen, char* pOut, int& iOutlen)
    {
          if(!pOut) return NULL;
           iOutLen = iInLen;       for(int i=0; i<iInLen; i++)
           {
                   pOut[iInLen-i-1] = pSource[i];
            }        return pOut;
    }
      

  20.   

    static void Main()
            {
                Antion();
            }
            static void Antion()
            {
                string s = "Welcome_to__Mircosoft!";
                for (int i = s.Length - 1; i >= 0; i--)
                {
                    Console.WriteLine(s[i]);
                }
            }