字符串里面的单词反转

字符串里面的单词反转,可以用string/stringBuilder

View Code
using System;
using System.Collections;
using System.Text;

namespace InsertionSorter
{
    public static class InsertionSorter
    {
        public static string ReverseString(string str)
        {
            string result = null;
            string[] s = str.Split(' ');
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = s[i].Length - 1; j >= 0; j--)
                {
                    result += (s[i])[j];
                }

                result += " ";
            }

            return result;
            //Result: siht si a .tset 
        }
        public static string StrReverse(string str)
        {
            StringBuilder sb = new StringBuilder();
            string[] s = str.Split(' ');
            for (int i = 0; i < s.Length; i++)
            {
                sb.Append(Reverse(s[i]) + " ");
            }

            return sb.ToString();
            //Result: siht si a .tset 
        }
        private static string Reverse(string str)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = str.Length - 1; i >= 0; i--)
            {
                sb.Append(str[i]);
            }

            return sb.ToString();
        }
    }
    public class MainClass
    {
        public static void Main()
        {
            string result = null;
            string thisString = "this is a test.";
            InsertionSorter.ReverseString(thisString);
            result = InsertionSorter.StrReverse(thisString);

            Console.WriteLine(result);

        }
    }
}

 

posted @ 2013-05-02 11:16  Binyao  阅读(148)  评论(0编辑  收藏  举报