I recently switched to “dark” vs2008 color scheme since it is easy on eyes.
I started with RagnaRok color scheme created by Tomas Restrepo and modified it to my convenience.
You can download the modified version from here: RagnaRok-modified.
I recently switched to “dark” vs2008 color scheme since it is easy on eyes.
I started with RagnaRok color scheme created by Tomas Restrepo and modified it to my convenience.
You can download the modified version from here: RagnaRok-modified.
I added the ability to rotate string right or left. Here is the code:
public enum Direction { Right, Left }
string rotateString(string source, int rotationCount, Direction direction)
{
if (String.IsNullOrEmpty(source) || rotationCount <= 0)
return source;
int pivot = 0;
List<int> delimiterLocations = new List<int>();
char[] sourceChars = new char;
char temp;
// Reverse the whole string and
// save the dilimiter locations
for (int i = 0; i < source.Length; i++)
{
sourceChars = source[i];
if (source[i] == ' ')
delimiterLocations.Add(source.Length - i - 1);
}
// calculate neededDelimiters mod wordCount
// (assume words = delimiterCount + 1)
pivot = rotationCount % delimiterLocations.Count;
if (pivot > 0)
{
if (direction == Direction.Left)
pivot = delimiterLocations.Count - pivot;
pivot = delimiterLocations[delimiterLocations.Count - pivot];
}
else
{ return source; }
// reverse the first part
for (int i = 0, j = pivot - 1; i <= j; i++, j--)
{
temp = sourceChars[i];
sourceChars[i] = sourceChars[j];
sourceChars[j] = temp;
}
// reverse the second part
for (int i = pivot + 1, j = sourceChars.Length - 1;
i <= j; i++, j--)
{
temp = sourceChars[i];
sourceChars[i] = sourceChars[j];
sourceChars[j] = temp;
}
return new string(sourceChars);
}
Someone asked the following question on StackOverflow. Before I could post my answer, it was removed. Hmm, so He/She tried to abuse StackOverflow! Anyway, I found that question interesting.
The question was to write a function to rotate a string of words. Words are separated by ‘ ‘ (space). In the result string, the words should remain human readable. The function will also take an integer argument that will specify number of words to rotate.
example:
string source = “A quick brown fox jumps over the lazy dog.”
rotateString(source, 3);
Should give:
the lazy dog. A quick brown fox jumps over
Here is my solution:
static string rotateString(string source, int rotationCount)
{
if (String.IsNullOrEmpty(source) || rotationCount <= 0)
return source;
int pivot = 0;
List<int> delimiterLocations = new List<int>();
char[] sourceChars = new char;
char temp;
// Reverse the whole string and
// save the dilimiter locations
for (int i = 0; i < source.Length; i++)
{
sourceChars = source[i];
if (delimiterLocations.Count < rotationCount && source[i] == ' ')
delimiterLocations.Add(source.Length - i - 1);
}
// calculate neededDelimiters mod wordCount
// (assume words = delimiterCount + 1)
pivot = rotationCount % (delimiterLocations.Count + 1);
if (pivot > 0)
pivot = delimiterLocations[pivot-1];
else
return source;
// reverse the first part
for (int i = 0, j = pivot - 1; i <= j; i++, j--)
{
temp = sourceChars[i];
sourceChars[i] = sourceChars[j];
sourceChars[j] = temp;
}
// reverse the second part
for (int i = pivot + 1, j = sourceChars.Length - 1;
i <= j; i++, j--)
{
temp = sourceChars[i];
sourceChars[i] = sourceChars[j];
sourceChars[j] = temp;
}
return new string(sourceChars);
}