Beispiel #1
0
        public string ReverseWords(string s)
        {
            if(s == null)
            {
                var fault = new ArgumentNullExceptionFault();

                fault.FaultMessage = "Value cannot be null.";

                throw new FaultException<ArgumentNullExceptionFault>(fault, fault.FaultMessage);
            }

            var wordArray = s.ToCharArray();
            char[] reversedWord = new char[wordArray.Length];
            //char[] temp = new char[wordArray.Length];
            var tempString = new StringBuilder();
            int rCounter = 0;
            var cond = true;

            for (var count = 0; count < wordArray.Length; count++)
            {
                if ((wordArray[count] == Convert.ToChar(" ")))
                {
                    if (count == wordArray.Length - 1)
                    {
                        reversedWord[rCounter] = wordArray[count];

                        if (tempString.Length > 0)
                        {
                            _ReverseWord(tempString, reversedWord, ref rCounter);

                            reversedWord[rCounter] = wordArray[count];
                            rCounter++;

                            tempString.Clear();
                        }
                    }
                    else
                    {
                        if (tempString.Length > 0)
                        {
                            _ReverseWord(tempString, reversedWord, ref rCounter);

                            reversedWord[rCounter] = wordArray[count];
                            rCounter++;

                            tempString.Clear();
                        }
                        else
                        {
                            reversedWord[rCounter] = wordArray[count];
                            rCounter++;
                        }
                    }
                    cond = false;
                }
                else
                {
                    cond = true;
                }

                if (cond == true)//no space encountered
                {
                    tempString.Append(wordArray[count]);

                    if (count == wordArray.Length - 1)
                    {
                        _ReverseWord(tempString, reversedWord, ref rCounter);
                        tempString.Clear();
                    }
                }
            }

            var output = new string(reversedWord);
            return output;
        }
Beispiel #2
0
        public string ReverseWords(string s)
        {
            if (s == null)
            {
                var fault = new ArgumentNullExceptionFault();

                fault.FaultMessage = "Value cannot be null.";

                throw new FaultException <ArgumentNullExceptionFault>(fault, fault.FaultMessage);
            }

            var wordArray = s.ToCharArray();

            char[] reversedWord = new char[wordArray.Length];
            //char[] temp = new char[wordArray.Length];
            var tempString = new StringBuilder();
            int rCounter   = 0;
            var cond       = true;

            for (var count = 0; count < wordArray.Length; count++)
            {
                if ((wordArray[count] == Convert.ToChar(" ")))
                {
                    if (count == wordArray.Length - 1)
                    {
                        reversedWord[rCounter] = wordArray[count];

                        if (tempString.Length > 0)
                        {
                            _ReverseWord(tempString, reversedWord, ref rCounter);

                            reversedWord[rCounter] = wordArray[count];
                            rCounter++;

                            tempString.Clear();
                        }
                    }
                    else
                    {
                        if (tempString.Length > 0)
                        {
                            _ReverseWord(tempString, reversedWord, ref rCounter);

                            reversedWord[rCounter] = wordArray[count];
                            rCounter++;

                            tempString.Clear();
                        }
                        else
                        {
                            reversedWord[rCounter] = wordArray[count];
                            rCounter++;
                        }
                    }
                    cond = false;
                }
                else
                {
                    cond = true;
                }

                if (cond == true)//no space encountered
                {
                    tempString.Append(wordArray[count]);

                    if (count == wordArray.Length - 1)
                    {
                        _ReverseWord(tempString, reversedWord, ref rCounter);
                        tempString.Clear();
                    }
                }
            }

            var output = new string(reversedWord);

            return(output);
        }