Tuesday, June 23, 2015

[algorithm][array/string] Reverse word order in a string and get the word count



http://seesharpconcepts.blogspot.com/2012/07/reverse-words-of-given-string-in-place.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WordOrderChange
{
    class Program
    {
        static char[] separators = { ' ' };

        static void Main(string[] args)
        {
            string[] testCases = {
                                    "It is sunny today",                    // regular
                                    "It  is sunny  today2",                 // mutiple spaces.
                                    "abc",                                  // single word
                                    " ",                                    // single space
                                    "          ",                           // multiple spaces
                                    "   I am Paul   ",                      // begin and end with spaces
                                    "It is sunny today. " + Environment.NewLine + "I like it.",        // string with enter
                                    string.Empty,                        
                                    null
                                 };
         
            // ReverseWordOrder1
            // This implementation uses the string.Split().
            // It removes the redundant spaces.
            Console.WriteLine("================= Implementation 1 =====================");
            foreach (var str in testCases)
            {
                int wordCount = 0;
                Console.WriteLine("Orignial string: {0}", str == null ? "null" : str);
                string changed = ReverseWordOrder1(str, out wordCount);
                Console.WriteLine("After reversed: {0}", changed == null ? "null" : changed );
                Console.WriteLine("Word count: {0}", wordCount);
                Console.WriteLine();
            }

            // ReverseWordOrder2
            // This implementation does not use string library fuctions.
            // It keeps the spaces if there are redundant.
            Console.WriteLine("================= Implementation 2 =====================");
            foreach (var str in testCases)
            {
                int wordCount = 0;
                Console.WriteLine("Orignial string: {0}", str == null ? "null" : str );
                string changed = ReverseWordOrder1(str, out wordCount);
                Console.WriteLine("After reversed: {0}", changed == null ? "null" : changed );
                Console.WriteLine("Word count: {0}", wordCount);
                Console.WriteLine();
            }
        }

        // implementation 1
        public static string ReverseWordOrder1(string str, out int wordCount)
        {
            if (str == null)
            {
                wordCount = -1;
                return str;
            }

            string[] strArr = str.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            string output = string.Empty;
            foreach (var s in strArr)
            {
                output = s + " " + output;
            }

            wordCount = strArr.Length;

            return output;
        }

        // implementation 2
        public static string ReverseWordOrder2(string str, out int wordCount)
        {
            if (str == null)
            {
                wordCount = -1;
                return str;
            }

            string output = PartialReverse(str, 0, str.Length-1);

            int begin = 0, end = 0;
            int count = 0;
            bool wordStartedFlag = false;
            while (end < output.Length)
            {
                if (output[end] != ' ' )
                {
                    if (!wordStartedFlag)
                    {
                        begin = end;
                        wordStartedFlag = true;
                    }

                    end++;
                    continue;

                }

                // current char is a space
                if (wordStartedFlag)
                {
   
                    output = PartialReverse(output, begin, end - 1);
                    count++;
   
   
                }

                wordStartedFlag = false;
                end++;
   
   

            }

            if ((begin < end - 1) && (wordStartedFlag))
            {
                output = PartialReverse(output, begin, end - 1);
                count++;
            }

            wordCount = count;

            return output;
        }


        public static string PartialReverse(string str, int begin, int end)
        {
            char[] charArr = str.ToCharArray();

            char temp;
            while (begin <= end)
            {
                temp = charArr[end];
                charArr[end] = charArr[begin];
                charArr[begin] = temp;
                begin++;
                end--;
            }

            return new string(charArr);
        }
    }
}

No comments:

Post a Comment