String reverse

Niladri.Biswas
Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 1633
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.

Comments or Responses

Posted by: anwarbesa-15403 on: 5/2/2012 Level:Starter | Status: [Member] | Points: 10
Hi.. that is good code

Thank you so much
Posted by: anwarbesa-15403 on: 5/2/2012 Level:Starter | Status: [Member] | Points: 10
But what we should do first?

I need complete program not only the methods!

Login to post response