While helping my office mate prepare for his Interviews a whole bunch of us were coming up with questions and challenges for him. We were really hoping he would make it. One of the challenges asked was to reverse a string.
Having been asked to do it in an interview before (I did it in VB then) I had, had the time to come up with other possible solutions to the challenge. Here is a C# version of the method from memory:
/// <summary>
/// Reverses a sequence of chars in a string.
/// </summary>
/// <param name="value">String to Reverse</param>
/// <returns>the reverse of the provided string.</returns>
/// <remarks>
/// Reverse by back stepping through each char in the string
/// and filling a StringBuilder with the result.
/// </remarks>
public string reverseStringFill(string value)
{
StringBuilder temp = new StringBuilder();
// Iterate backwards through each char and copy
// it's equivalent from the source string.
for (int index = value.Length - 1; index >= 0; index--)
{
temp.Append(value[index]);
}
return temp.ToString();
}
Here is another variation:
/// <summary>
/// Reverses a sequence of chars in a string.
/// </summary>
/// <param name="value">String to Reverse</param>
/// <returns>the reverse of the provided string.</returns>
/// <remarks>
/// Reverse by back stepping through each char in the string
/// and filling the resulting array.
/// </remarks>
public string reverseStringBFill(string value)
{
char[] temp = value.ToCharArray();
int length = value.Length - 1;
// Iterate backwards through each char and copy
// it's equivalent from the source string.
for (int index = length; index >= 0; index--)
{
temp[index] = value[length - index];
}
return new string(temp);
}
My Office mate having learned in C/C++ most of school came up with a swap method that he had learned in class, but when he tried to run it in C# it had compile errors. because he was trying to do things the C way in a type safe language. After a hint about casting he figured it out. What he ended up with is something similar to the following:
/// <summary>
/// Reverse String using Swap
/// </summary>
/// <param name="value">String to Reverse</param>
/// <returns>the reverse of the provided string.</returns>
/// <remarks>
/// This is how a C/C++ programmer would reverse a string in C#
/// </remarks>
public string reverseStringSwap(string value)
{
char[] returns = value.ToCharArray();
int index = 0;
while (index < value .Length / 2)
{
// Swap chars
char temp = returns[index];
returns[index] = returns[(value.Length-1) - index];
returns[(value.Length-1) - index] = temp;
index++;
}
return new string(returns);
}
While he worked at the problem I was thinking of a similar way to do the same thing from a managed perspective:
/// <summary>
/// Reverses a sequence of chars in a string.
/// </summary>
/// <param name="value"></param>
/// <returns>the reverse of the provided string.</returns>
/// <remarks>
/// This is similar to the swap method except it
/// only pulls it's chars from the source string.
/// </remarks>
public string reverseStringBSwap(string value)
{
char[] temp = value.ToCharArray();
int length = value.Length - 1;
int half = length / 2;
for (int index = 0; index < length; index++)
{
temp[index] = value[length - index];
temp[length - index] = value[index];
}
return new string(temp);
}
He had written his in a console app and I in a GUI app so rather than return new string(returns); His actually used Console.Write(returns); I stumbled around for a little while trying to remember how to cast to a string. But eventually found it, hopefully I can remember it now. I have changed the methods so they work the same in a GUI and a Console app as I like a clean Modular design.
While at it I found Array.Reverse(""); and this nice little, simple, clean and tidy, method ensued:
/// <summary>
/// Reverses a sequence of chars in a string.
/// </summary>
/// <param name="value"></param>
/// <returns>the reverse of the provided string.</returns>
/// <remarks>
/// This is the simple way to reverse a string.
/// Simply by using the power of the Framework.
/// </remarks>
public string reverseStringArray(string value)
{
// Copy to an array
char[] temp = value.ToCharArray();
// Reverse
Array.Reverse(temp);
// Return casting the array as a string
return new string(temp);
}
Now for the real fun. Recursion (Where a method calls it's self over and over until it finishes it's work, then backs out to provide the results.). This method ends up being fairly simple, but it is slower, has potential to crash and is somewhat difficult to wrap one's head around. But it looks clean and impressive.
/// <summary>
/// Recursive String Reverse
/// </summary>
/// <param name="value">String to Reverse</param>
/// <returns>the reverse of the provided string.</returns>
/// <remarks>
/// This method Reverses a string by calling it's self recusively
/// The method used here is certainly not the fastest method avalible
/// as it uses slow string concatenation and has the possibility of
/// having a buffer overflow recursing to many levels if using a
/// string which is to long. But the idea is just so fun.
/// </remarks>
public string reverseStringRecursive(string value)
{
if (value != "")
{
// Recurse the incoming string, minus the first char, then return
// the resulting string, appending the first char to the end.
return reverseStringRecursive(value.Remove(0,1)) + value[0];
}
else
{
return "";
}
}
Ah, this was fun! One of these days I should revisit one of my old favorite sites (http://www.xbeat.net/vbspeed/) and try to work out a method for timing the methods and try a few other speedy ideas.
One of the odd things from that site is that you learn that simplicity is not always equal to speed. Rather often the reverse is true. There are some strangely long code snippets setup just for improvement with speed.