Random Noise

cat /dev/random >> /dev/dsp

StackOverflow – RotateString

with 4 comments

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[source.Length];

    char temp;
    // Reverse the whole string and
    // save the dilimiter locations
    for (int i = 0; i < source.Length; i++)
    {
        sourceChars[source.Length - i - 1] = 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);
}
Advertisements

Written by Vivek Unune

September 25, 2008 at 5:04 am

Posted in .NET

Tagged with ,

4 Responses

Subscribe to comments with RSS.

  1. how about this

    function rotate(input, num) {
    return (input+input).SubString(num, input.Length);
    }

    sijinjoseph

    October 1, 2008 at 4:53 pm

  2. Thanks for stopping by.

    The point is, ‘num’ represents words in the string to rotate. And words are delimited by ‘ ‘ space.

    So if I give “A quick brown fox jumps over the lazy dog.” and 3 as input. The result according to your snippet will be:

    “uick brown fox jumps over the lazy dog.A q”

    We need “fox jumps over the lazy dog. A quick brown”

    This was posted on stackoverflow.com as an interview question so I haven’t used any library functions.

    Vivek Unune

    October 1, 2008 at 5:06 pm

  3. I’m sure it would be a bit easier to use String.Split to get an array of words.

    Mladen Mihajlovic

    October 23, 2008 at 10:41 am

  4. Mladen,

    Thanks for your comment.

    Yes, String.Split will definitely ease things up. But since it was posted as an interview question, I stayed away from library functions provided by .NET.

    I’m interested to know a better solution that one in my post.

    Vivek Unune

    October 23, 2008 at 11:24 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: