lbrahim lbrahim - 1 year ago 91
C# Question

Reverse elements of a string array

string[] myString = {"a","b","c","d"}



//Reverse string algorithm here



myString = {"d","c","b","a"}



I have been asked to do so in an interview without the help of any temporary variable or .NET class, string methods, etc to reverse the elements of the same string array. I was told to use basic programming constructs like loops. Since, I am up for another interview today, I am in a hurry to know whether this is actually possible because I could not find a solution to this.

Answer Source

Here you go, no support variables, no .net functions :) But it makes the assumptions that all the strings in the array have length 1 ( as they do in the code you posted).

string[] myString = {"a","b","c","d", "e"};
for(int i = 0; i < myString.Length/2; i++)
{
   myString[i] += myString[myString.Length -1 -i];
   myString[myString.Length -1 -i] = ""+myString[i][0];
   myString[i] = "" + myString[i][1];
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download