次の方法で共有


Reverse a string in different ways

Hi folks,

I am here with another example, how to reverse a string in different ways?

Below are the different ways to do it.

 

/// Need one extra array for result, need to traverse full array.

public static string ReverseString1 (string str)
        {
            char[] chars = str.ToCharArray();
            char[] result = new char[chars.Length];

            for (int i = 0, j = str.Length - 1; i < str.Length; i++, j--)
            {
                result[i] = chars[j];
            }

            return new string(result);
        }

-----------------------------------------------------------------

/// Uses swap method to reverse; need to traverse only half of the array.

public static string ReverseString2(string str)
        {
            char[] chars = str.ToCharArray();
            for (int i = 0, j = str.Length - 1; i < j; i++, j--)
            {
                char c = chars[i];
                chars[i] = chars[j];
                chars[j] = c;      
            }
            return new string(chars);
        }

------------------------------------------------------------------

/// Here is the use of in-place swap without any temp variable

public static string ReverseString3 (string str)
        {
            char[] chars = str.ToCharArray();
            for (int i = 0, j = str.Length - 1; i < j; i++, j--)
            {
                chars[i] = str[j];
                chars[j] = str[i];
            }
            return new string(chars);
        }

---------------------------------------------------------------------

/// String Reversal without Copy to Char Array it's i <= j as we need to get the middle character in case of odd number of characters in the string

public static string ReverseString3b(string str)
        {
            char[] chars = new char[str.Length];
            for (int i = 0, j = str.Length - 1; i <= j; i++, j--)
            {
                chars[i] = str[j];
                chars[j] = str[i];
            }
            return new string(chars);
        }

--------------------------------------------------------------------

 /// String reversal with stack [Please note here Stack_Array is my custom Stack class, you can replace this with provided by .NET]

public static string ReverseString4(string str)
        {
            Stack_Array stk1 = new Stack_Array(str.Length);
            foreach (char c in str)
                stk1.Push(c);
            string revString = null;
            foreach (char c in str)
                revString += stk1.Pop();
            return revString;
        }

-------------------------------------------------------------------

/// String reversal with XOR (^); interesting way to reversal
/// A [i] = A[i] ^ A[len] -> A[i]  = 80 ^ 73 -> A[i] = 25
/// A [len] = A[len] ^ A[i] -> A[len]  = 73 ^ 25 -> A[len] = 80
/// A [i] = A[i] ^ A[len] -> A[i]  = 25 ^ 80 -> A[i] = 73

public static string ReverseString5(string str)
        {
            char[] inputstream = str.ToCharArray();
            int length = str.Length - 1;
            for (int i = 0; i < length; i++, length--)
            {
                inputstream[i] ^= inputstream[length];
                inputstream[length] ^= inputstream[i];
                inputstream[i] ^= inputstream[length];
            }
            return new string(inputstream);
        }

-------------------------------------------------------------------

 /// Recursion method; simple and regular performance for small strings

public static string ReverseString_Rec(string str)
        {
            if (str.Length <= 1)
                return str;
            else
                return ReverseString_Rec(str.Substring(1)) + str[0];
        }

------------------------------------------------------------------

/// Another way of recursion; need to pass index as 0

public static string ReverseString_Rec2(string str, int index)
        {
            char[] chars = str.ToCharArray();
            int len = chars.Length;
            if (index < len / 2)
            {
                char c = chars[index];
                chars[index] = chars[len - index - 1];
                chars[len - index - 1] = c;
                index++;
                return ReverseString_Rec2(new string (chars), index);
            }
            else
            {
                return new string(chars);
            }
        }

 

Please let me know your comments/ suggestions or if you have better ways :)

Comments

  • Anonymous
    October 23, 2014
    third way (swap) showing an exception