string s = "I am a student"; string[] words = s.Split(' '); Array.Reverse(words); string result = string.Join(" ", words);
string str = "i am a student "; string[] temp = str.Split(' '); for (int i = temp.Length - 1; i >= 0; i--) { Console.Write("{0} ", temp[i]); } Console.Read();
Array.Reverse(words); 原来可以这样啊,学习ing
private string Reverse(string s) { char[] cs = s.ToCharArray(); int end = s.Length - 1; int length = 0; string result = string.Empty; while(end >= 0) { while(end >= 0 && cs[end] != ' ') { length++; end --; } result += new string(cs, end+1, length); result += " "; length = 0; end --; } return result; }
string s = "I am a student";
string[] words = s.Split(' ');
Array.Reverse(words);
string result = string.Join(" ", words);
string[] temp = str.Split(' '); for (int i = temp.Length - 1; i >= 0; i--)
{
Console.Write("{0} ", temp[i]);
}
Console.Read();
原来可以这样啊,学习ing
{
char[] cs = s.ToCharArray();
int end = s.Length - 1;
int length = 0;
string result = string.Empty;
while(end >= 0)
{
while(end >= 0 && cs[end] != ' ')
{
length++;
end --;
}
result += new string(cs, end+1, length);
result += " ";
length = 0;
end --;
}
return result;
}