((c#))替换空格

   class MyArray
    {
        static void Main(string[] args)
        {
            String str = "heros never die";
           char[] newss=Str(str);
            for (int i = 0; i < newss.Length; i++)
            {
                Console.Write(newss[i]);
            }
        }
        public static char[] Str(string str)
        {
            char[] oldstr = str.ToCharArray();
            char[] newstr = new char[100];
            for (int i = 0; i < oldstr.Length; i++)
            {
                newstr[i] = oldstr[i];
            }
            int black = 0;
            for (int j = 0; j < newstr.Length; j++)
            {
                if (newstr[j] == ' ')
                    black++;
            }
            int front = oldstr.Length - 1;
            int back = oldstr.Length + 2 * black - 1;
            while (front>=0&&back>=0)
            {
                if (newstr[front] != ' ')
                {
                    newstr[back--] = newstr[front];
                }
                else
                {
                    newstr[back--] = '0';
                    newstr[back--] = '2';
                    newstr[back--] = '%';
                }
                front--;
            }return newstr;
        }
    }

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页