Answer

Simplest way to approach reverse string problems is to treat string as an array of characters and switch chars one by one.

Solution in C# which yields O(n) execution time.

static public string Reverse(string str)
{
char[] charArray = str.ToCharArray();
int len = str.Length - 1;

for (int i = 0; i < len; i++, len--)
{
charArray[i] ^= charArray[len];
charArray[len] ^= charArray[i];
charArray[i] ^= charArray[len];
}

return new string(charArray);

}







Answers and Comments