What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 38281 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > String reverse ...
Niladri.Biswas

String reverse

 Code Snippet posted by: Niladri.Biswas | Posted on: 4/30/2012 | Category: C# Codes | Views: 668 | Status: [Member] | Points: 40 | Alert Moderator   


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.

Best Regards,
Niladri Biswas
Found interesting? Add this to:


 Responses

Anwarbesa@Yahoo.Com
Posted by: Anwarbesa@Yahoo.Com | Posted on: 5/2/2012 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

Hi.. that is good code

Thank you so much

C is a sea.. and you are the sea of C!

Thanks & Regards

Anwarbesa@Yahoo.Com
Posted by: Anwarbesa@Yahoo.Com | Posted on: 5/2/2012 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

But what we should do first?

I need complete program not only the methods!

C is a sea.. and you are the sea of C!

Thanks & Regards

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 9:42:40 AM