Reverse a sentence quickly without pointers I am trying to reverse a sentence contained in a string and return a string in the quickest way possible using the least amount of memory. Also I don't want to use any unsafe code, so no pointers are allowed. Please let me know if anything can be improved. 

My input example is


string mystring = "Hello! my name is";



My result is 


  "is name my Hello!"


My results on i7 4770S for 1000000 iterations is on average around 650ms.

public static string ReverseTheString(string MyString)
{

    int Length = MyString.Length;
    Char[] NormalArray = new char[Length];
    Char[] FinalArray = new char[Length];

    for (int i = 0; i &lt; Length; i++)
    {
        NormalArray[i] = MyString[i];
    }
    Length = Length - 1; //use for last index
    int SpacesCount = 0;
    int AlphaCount = 0;

    Stack&lt;char[]&gt; ReversedArray = new Stack&lt;char[]&gt;();

    for (int i = 0; i &lt; NormalArray.Length; i++)
    {
        if (NormalArray[i] == ' ' &amp;&amp; i != Length)//Space
        {
            if (i != Length)
            {
                if (AlphaCount &gt; 0)
                {
                    char[] temparray = new char[AlphaCount];
                    int tempindex = i - AlphaCount;
                    for (int j = tempindex, k = 0; k &lt; AlphaCount; j++, k++)
                    {
                        temparray[k] = NormalArray[j];
                    }

                    ReversedArray.Push(temparray);
                    AlphaCount = 0;
                    temparray = null;
                }

                SpacesCount++;
            }
            else
            {

                SpacesCount++;
                if (SpacesCount &gt; 0)
                {
                    char[] temparray = new char[SpacesCount];
                    int tempindex = i + 1 - SpacesCount;
                    for (int j = tempindex, k = 0; k &lt; SpacesCount; j++, k++)
                    {
                        temparray[k] = NormalArray[j];
                    }

                    ReversedArray.Push(temparray);
                    SpacesCount = 0;
                    temparray = null;
                }

            }
        }

        if (NormalArray[i] != ' ' ) //alpha
        {
            if (i != Length)
            {
                if (SpacesCount &gt; 0)
                {
                    char[] temparray = new char[SpacesCount];
                    int tempindex = i - SpacesCount;
                    for (int j = tempindex, k = 0; k &lt; SpacesCount; j++, k++)
                    {
                        temparray[k] = NormalArray[j];
                    }

                    ReversedArray.Push(temparray);
                    SpacesCount = 0;
                    temparray = null;
                }
                AlphaCount++;
            }
            else
            {
                AlphaCount++;
                if (AlphaCount &gt; 0)
                {
                    char[] temparray = new char[AlphaCount];
                    int tempindex = i + 1 - AlphaCount;
                    for (int j = tempindex, k = 0; k &lt; AlphaCount; j++, k++)
                    {
                        temparray[k] = NormalArray[j];
                    }

                    ReversedArray.Push(temparray);
                    AlphaCount = 0;
                    temparray = null;
                }
            }
        }
    }


    int Pos = 0;
    while (ReversedArray.Count &gt; 0)
    {
        char[] temparray = ReversedArray.Pop();

        for (int j = 0; j &lt; temparray.Length; j++)
        {
            FinalArray[Pos] = temparray[j];
            Pos++;
        }
    }

    return new string(FinalArray);
}
The shortest code I can produce is:

private static string Reverse(string input)
{
    return String.Join(" ", input.Split(' ').Reverse());
}


But the following alternative should be much faster:

private static string Reverse(string input)
{
    // Create a new StringBuilder and allocate the same space as in the input string:
    StringBuilder builder = new StringBuilder(input.Length);

    // Declare 2 index variables:
    int index;
    int lastIndex = input.Length;

    // Iterate from the end to the start of the string
    // while the ' ' char is found:
    while ((index = input.LastIndexOf(' ', lastIndex - 1)) != -1)
    {
        builder.Append(input, index + 1, lastIndex - index - 1).Append(' ');
        lastIndex = index;
    }
    // Last iteration (for the first token in the input string):
    if (lastIndex != index)
    {
        builder.Append(input, 0, lastIndex);
    }
    return builder.ToString();
}


The main idea is to iterate from the end of the input string to the beginning, looking for the space char.
Also it makes sense to rely on the .NET built-in methods as much as possible.
Therefore I used:  


The StringBuilder class for accumulation of a result.
The String.LastIndexOf method for searching for the space char.


The reasons to use the StringBuilder:


It is very effecient.
It contains the Append(string value, int startIndex, int count) method which copies specified substring to the inner buffer directly (without creating an intermediate substring from the input string).


EDIT.
A new approach not using the StringBuilder class and with the same main idea:

private static string Reverse(string inputString)
{
    // Get array of chars from the inputString,
    // and allocate the same space in the output array:
    char[] input = inputString.ToCharArray();
    char[] output = new char[input.Length];

    // Declare 3 index variables:
    int index;
    int lastIndex = input.Length;
    int outputIndex = 0;

    // Iterate from the end to the start of the string
    // while the ' ' char is found:
    while ((index = inputString.LastIndexOf(' ', lastIndex - 1)) != -1)
    {
        Array.Copy(input, index + 1, output, outputIndex, lastIndex - index - 1);
        outputIndex += lastIndex - index;
        output[outputIndex - 1] = ' ';
        lastIndex = index;
    }
    // Last iteration (for the first token in the input string):
    if (lastIndex != index)
    {
        Array.Copy(input, 0, output, outputIndex, lastIndex);
    }
    return new string(output);
}


It is slightly less efficient than previous approach, but it is still very fast.