Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Reverse A String In C# With And Without An Inbuilt Function

See the details example to Reverse A String In C# With And Without An Inbuilt Function.


For example,

namespace DemoExample

{

    class Program: ReverseString

    {

        static void Main(string[] args)

        {

            ReverseString objReverse = new ReverseString(); 

            //reverse a string with Inbuilt function in C#

            // With Inbuilt Method Array.Reverse Method

            string resultReverseAStringWith = objReverse.ReverseAStringWithReverseMethod("ANILKUMARSINGH");//Output -"HGNISRAMUKLINA"

 

            //reverse a string without Inbuilt function in C#

            //Using While Loop

            string resultReverseAStringWithout = objReverse.ReverseAStringWithoutReverseMethod("ANILKUMARSINGH");//Output -"HGNISRAMUKLINA"  

        }

      

    }

 

    class ReverseString

    {

        // Reverse a string with Inbuilt function in C#

        // With Inbuilt Method Array.Reverse Method

        public stringReverseAStringWithReverseMethod(string str)

        {

            char[] chars = str.ToCharArray(); 

            Array.Reverse(chars);

            return new string(chars);           

        }

 

        //Reverse a string without Inbuilt function in C#

        //Using While Loop

        public string ReverseAStringWithoutReverseMethod(string str)

        {

            string reverseStr = string.Empty;

            int length= str.Length - 1; 

            while (length >=0)

            {

                reverseStr = reverseStr + str[length];

                length--;

            }

            return reverseStr;

        }

    }

}



This post first appeared on Programming, please read the originial post: here

Share the post

Reverse A String In C# With And Without An Inbuilt Function

×

Subscribe to Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×