如何将此方法转换为递归方法?我无法定义基本条件和结束条件。如何减少或增加结束条件以满足基本条件?
private string ReverseString(string s)
{
char[] array = new char[s.Length];
int index = 0;
for (int i = s.Length - 1; i >= 0; i--)
{
array[index++] = s[i];
}
return new string(array);
}
使用stringbuilder
private string ReverseLetter(string letter)
{ //copy to stack
Stack<char> stack = new Stack<char>();
foreach (char ch in letter)
stack.Push(ch);
//read the stack into a string
StringBuilder sb = new StringBuilder(10);
while (stack.Count > 0)
{
sb.Append(stack.Pop());
}
return sb.ToString();
}