反转一句话(完全反转各字符)

目的:将字串"I am  a  programmer"完全反转为"remmargorp   a  ma I"。

方法一:

        private static void reverse()
        {
            string str = "I am  a   programmer";

            for (int i = str.Length -1; i >=0; i--)
            {
                Console.Write(str[i]);
            }

            Console.WriteLine();
        }

方法二:

        private static void reverse()
        {
            string str = "I am  a   programmer";
            char[] arr = new char[str.Length];

            for (int i = 0; i < str.Length; i++)
            {
                arr[i] = str[i];
            }

            Array.Reverse(arr);

            for (int j = 0; j < arr.Length; j++)
            {
                Console.Write(arr[j]);
            }

            Console.WriteLine();
        }

 

posted on 2008-08-05 15:09  Good life  阅读(392)  评论(0编辑  收藏  举报