23 May 2016 Sudhanshu Ranjan 617

How to reverse a String using C#

There is couple's of way to reverse the string using c# programming.

 

1. Using With reverse function

public string ReverseString(string s)
            {
                char[] arr = s.ToCharArray();
                Array.Reverse(arr);
                return new string(arr);
            }

 

2. Using While Loop

public string ReverseString(string s)
            {
                String RevStr = "";
                int Length;
                Length = s.Length - 1;
                while (Length >= 0)
                {
                    RevStr = RevStr + s[Length];
                    Length--;
                }
                return RevStr;
            }

 

3. Using For Loop

 public String ReverseString(string s)
            {
                string RevStr = "";
                for (int i = s.Length - 1; i >= 0; i--)
                {
                    RevStr += s[i];
                }
                return RevStr;
            }

 

4.  Using LINQ

public String ReverseString(string input)
            {
                string output = new string(input.ToCharArray().Reverse().ToArray());
                return output;
            }

 

5. Using String Builder

public String ReverseString(string input)
            {
                char[] characters = input.ToCharArray();
                StringBuilder sb = new StringBuilder();
                for (int i = input.Length - 1; i >= 0; --i)
                {
                    sb.Append(characters[i]);
                }

                return sb.ToString();
            }

You can share at :

About Author
Sudhanshu Kumar Ranjan | Lead Engineer at HCL Technologies | Expertise in Microsoft Technology | Author | Blogger | Programmer
Know More
Connect with him on Facebook | Google+ | LinkedIn | Twitter

Recommended Post

Leave a comment

About Dotnet Cube

It's my blog - Dotnet Cube, where you will learn and get solutions about programming, web designing and development. I started this website to share my knowledge and experience which might help someone.

You can get more details about me and my blog at About Us page.

Subscribe Newsletter

Subscribe to our monthly newsletter to get the latest industry news, Media Group updates and our current specials.

Follow On Us...

Home | About | Advertise | Copyrights | Privacy | Contact | Admin

Copyrights © 2015-2017 dotnetcube.com. All rights reserved