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.
Join them; it only takes a minute:
|
|||||||||||||||||||||
|
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).
|
|||||||||||||||||
|
Since you cannot use a temporary variable, the best I can think of is appending the strings first and then removing the appended part again:
Stupid approach? Yes. But no additional variables are involved. |
|||||||||||||||||||||
|
Sorry I posted a wrong answer... here's the verified one:
However, just consider this a pseudo-code... I did not check for .NET syntax. |
|||
|
This is not a complete answer, perhaps an idea.. It is possible to swap two numbers by mathematics
Initially I was going to suggest this can be done with character ascii values.. Then I noticed the strings. Is it acceptable then to
I hope you get the idea |
|||
|
Have you tried this
|
||||
|
If the maximum string length is of array is less than 10, then is might be helpful....
|
|||
|
Try this.
Example usage
|
|||
|
Yes you can do it-
|
|||||
|
You need at least one variable to swap values. In pseudo code:
|
|||||
|
|
|||||
|
We Can use |
|||||||||||||
|
Check out Array.Reverse method on MSDN. |
|||