您的位置:首页 > 其它

字符串里面的单词反转

2013-05-02 11:16 260 查看
字符串里面的单词反转,可以用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);

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: