Thursday, April 13, 2017

How do we reverse a string?



There are 2 ways of doing this

using array reverse function or by converting the string to character array




 //function to reverse a string
        public string Reverse(string text)
        {

            //Convert the 
            char[] cArray = text.ToCharArray();

            string reverse = string.Empty;
            for (int i = cArray.Length - 1; i >= 0; i--)
            {
                reverse = reverse + cArray[i];

            }
            return reverse;
        }
        #endregion
        //function to reverse using array.reverse
        public string ArrayReverse(string text)
        {
            //Convert the 
            char[] cArray = text.ToCharArray();
            Array.Reverse(cArray);
              return new string( cArray);
        }

No comments:

Post a Comment