C# An Alternate Reverse Sentence Routine – Comments Please

Question:
Implement a function that reverses a sentence in the following way:  “Hello, my name is Yoshiyuki” ->  “Yoshiyuki is name my ,Hello”.

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int count;
           
            string msg = "Hello, my ,name .is ,Yoshiyuki Aoyagi.";
            Console.WriteLine("OrgMsg: {0} \n", msg);
            string[] MsgArray = msg.Split(‘ ‘);
            count = MsgArray.Length;
            string[] OutMsgArray = new string[count];
            int k = 0;
            for (int i = count; i > 0; i–)
            {
                if (MsgArray[i – 1].EndsWith(",") | MsgArray[i – 1].EndsWith("."))
                {
                    int j = MsgArray[i – 1].Length;
                    string StrTemp = MsgArray[i – 1].Substring(j – 1);
                    OutMsgArray[k] = StrTemp + MsgArray[i – 1].Remove(j – 1, 1);
                }
                else
                {
                    OutMsgArray[k] = MsgArray[i – 1];
                }
                k++;
            }
            string temp4 = string.Join(" ", OutMsgArray);
            Console.WriteLine(temp4);
            Console.WriteLine("Press Enter to terminate……");
            Console.ReadLine();
           
        }
    }
}
Advertisements

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