Thursday, July 19, 2012

Reverse words of a given string - In place C# function without using string operations

Problem:

Given a string like "No, I'm Not Manu M S" print the words of the string in reverse order. Output: "S M Manu Not I'm No,". Do not use another string or stack or extra storage for doing this.

Solution:

We will write plain C style code without using any C# string method. Lets assume, words are separated by a space. Lets ignore the case wherever other symbols can separate the words( for our simplicity). We are asked to reverse the words but not the string reversal itself.

Our method follows below steps,

1. Find the length of a string. Bear with me to use Length property of string, since it is straight forward , lets not even code how to find the length also.

2. Reverse the string in-place, for each character in string from 0 to length/2 swap character at [length - i] with character at [i]. Remember to exit this loop at length/2, otherwise string gets reversed again. i.e. after length/2 steps we will have the reversed string with us.

3. For each word in the reversed string, reverse the characters. This step is not O(n) since we loop for the letters of a word inside the loop which loops through each character of reversed string. Inner loop is to reverse letters of each word of the reversed string. Make sure indexes are incremented carefully, to consider index of space, last index of space etc.

4. Print the string now. We have the words reversed.


Code:

       public static void ReverseWords(char[] words)
        {
            //O(n)
            int lenght = words.Length - 1;
            char temp ='o'; 

            //O(n/2)
            for (int i = 0; i <= lenght/2; i++)
            {
                //swap char at n-i with i
                temp = words[i];
                words[i] = words[lenght - i];
                words[lenght - i] = temp;
            }

            int wordStartIndex = 0;
            int wordEndIndex = 0;
            for (int i = 0; i <= lenght; i++)
            {
                if (words[i] == ' '  || i== lenght)
                {
                    //special care for last word
                    if (i != lenght)
                        wordEndIndex = i - 1;
                    else
                        wordEndIndex = lenght;
                    for (int j = wordStartIndex; j <= (wordStartIndex + wordEndIndex) / 2; j++)
                    {
                        //swap char at n-i with i
                        
                        temp = words[j];
                        words[j] = words[wordEndIndex + wordStartIndex- j];
                        words[wordEndIndex + wordStartIndex - j] = temp;
                    }
                    wordStartIndex = i + 1;
                }
            }

            //print the reversed words in given string
            for (int i = 0; i <= lenght; i++)
            {
                Console.Write(words[i]);
            }
            Console.WriteLine();
        }

No comments:

Post a Comment