如何将这种方法转换为递归方法?我在定义基本条件和结束条件时遇到了麻烦。如何减少或增加最终条件以满足基本条件?
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);
}
使用字符串生成器
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();
}