I have come up with some methods by which we can reverse a string. They are as under
Methods1
private string Reverse(string input)
{
return input.Reverse()
.Aggregate(new StringBuilder(), (sb, chars) => sb.Append(chars))
.ToString();
}
Methods2
private string Reverse(string input)
{
return input.Reverse().Aggregate("", (a, b) => a + b);
}
Methods3
private string Reverse(string input)
{
return new string(input.Reverse().ToArray());
}
Methods4
private string Reverse(string input)
{
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
Methods5
private string Reverse(string input)
{
int length = input.Length;
string reverseString = string.Empty;
for (int i = length; i > 0; i--)
{
reverseString += input[i-1];
}
return reverseString;
}
The invocation part is as under
var result = Reverse("abcdefghijklmnopqrstuvwxyz");
Hope this will be helpful.