先这样看看,楼主找给测试速度的方法,比较一下 private string Reverse(string str) { char[] src = str.ToCharArray(); char[] result = new char[str.Length]; int j = 0; for (int i = 0; i <= str.Length; i++) { if (i == str.Length || src[i] == ' ') { Array.Copy(src, j, result, str.Length - i, i - j); if (i < str.Length) result[str.Length - i - 1] = ' '; j = i + 1; } } return new string(result); }private void button1_Click(object sender, EventArgs e) { string s = "The quick brown fox jumps over the lazy dog."; Console.WriteLine(Reverse(s)); }
string a="The quick brown fox jumps over the lazy dog."; string[] arr = a.Split(' '); Array.Reverse(arr); Console.WriteLine(string.Join(" ",arr));
string[] arr = a.Split(' ');
Array.Reverse(arr);
Console.WriteLine(string.Join(" ",arr));
{
string temp=string.Empty;
string result=string.Empty;
foreach(char ch in sentence)
{
if(ch==' ')
{
result=temp+" "+result;
temp="";
}
else temp+=ch.ToString();
}
result=temp+" "+result;
return result;
}
private string Reverse(string str)
{
char[] src = str.ToCharArray();
char[] result = new char[str.Length];
int j = 0;
for (int i = 0; i <= str.Length; i++)
{
if (i == str.Length || src[i] == ' ')
{
Array.Copy(src, j, result, str.Length - i, i - j);
if (i < str.Length) result[str.Length - i - 1] = ' ';
j = i + 1;
}
} return new string(result);
}private void button1_Click(object sender, EventArgs e)
{
string s = "The quick brown fox jumps over the lazy dog.";
Console.WriteLine(Reverse(s));
}
string[] arr = a.Split(' ');
Array.Reverse(arr);
Console.WriteLine(string.Join(" ",arr));