I am trying to learn recursion and have a question involving an array that needs to be reversed. I am focusing on c# but the language probably doesn't matter a whole lot because my function is not using any libraries.
Here is my code:
char[] ReverseString(char[] s, int i = 0)
{
char[] x = s;
char temp = s[i];
s[i] = s[s.Length - (i + 1)];
s[s.Length - (i + 1)] = temp;
i++;
if (i < (s.Length) / 2)
{
ReverseString(s, i);
}
return x;
}
Any suggestions on how I should improve my function (maybe time complexity or using libraries (nuget packages)) is appreciated as well.
System.Linqis great when you want to do something with arrays or collectionsstring hello = "Hello World!"; string olleh = new string(hello.Reverse().ToArray());Output:!dlroW olleH. \$\endgroup\$