Thursday, 29 June 2017

Get start-date and last-date based on current date in C#

There is many way to find startDate and LastDate of the week , so i have used two way to find out and print it as below.

In first example i will use simple switch case to get startDate of the week.

       private void button3_Click_1(object sender, EventArgs e)
          {
            string dayOfWeek = DateTime.Today.DayOfWeek.ToString().ToLower();
            int getValue = 0;
            switch (dayOfWeek)
            {
                case "monday":
                    getValue = 0;
                    break;
                case "tuesday":
                    getValue = 1;
                    break;
                case "wednesday":
                    getValue = 2;
                    break;
                case "thursday":
                    getValue = 3;
                    break;
                case "friday":
                    getValue = 4;
                    break;
                case "saturday":
                    getValue = 5;
                    break;
                case "sunday":
                    getValue = 6;
                    break;
            }
            DateTime startWeek = DateTime.Now.AddDays(-getValue); // Need to subtract getValue
            DateTime endWeek = startWeek.AddDays(6); // Need to add

           // Print Date of the week.

           for (int i = 0; i <= 7; i++)
            {
                DateTime dt = startWeek;
                switch (i)
                {

                    case 0:
                        Label1.Text = dt.ToShortDateString();
                        break;
                    case 1:
                        Label2.Text = dt.AddDays(i).ToShortDateString();
                        break;
                    case 3:
                        Label3.Text = dt.AddDays(i - 1).ToShortDateString();
                        break;
                    case 4:
                        Label4.Text = dt.AddDays(i - 1).ToShortDateString();
                        break;
                    case 5:
                        Label5.Text = dt.AddDays(i - 1).ToShortDateString();
                        break;
                    case 6:
                        Label6.Text = dt.AddDays(i - 1).ToShortDateString();
                        break;
                    case 7:
                        Label7.Text = dt.AddDays(i - 1).ToShortDateString();
                        break;
                }

            }

}

Here is second example to get  startDate and LastDate of the week, this below example is simple and sweet .

DayOfWeek dayOf = DateTime.Now.DayOfWeek;

/* DayOfWeek  => is a Enum , its come under System.Runtime.InteropServices Namespace*/

int day = dayOf - DayOfWeek.Monday; // here we are assuming starting day is Monday  and subtracting current day of the week .

DateTime startDateTime = DateTime.Now.AddDays(-day);

DateTime endDateTime = DateTime.Now.AddDays(day);

  WriteLine($"StartDate if Week is {startDateTime.ToLongDateString()} and End date of week is {endDateTime.ToLongDateString()}");

Thursday, 15 June 2017

Find All vowels in string using C#

public static void Main()
        {
            int size = int.Parse(ReadLine());
            var inputValue = "";
            for (int i = 0; i < size; i++)
            {
                inputValue += Console.ReadLine();
            }
            var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
            bool findVowels = false;
            var findCharGroup = inputValue.GroupBy(x => x).Select(x => new
            {
                key = x.Key,
                Count = x.Count()
            }).ToDictionary(t => t.key, t => t.Count);

            foreach (var itemVowels in vowels)
            {

                if (findCharGroup.ContainsKey(itemVowels))
                {
                    findVowels = true;
                }
                else
                {
                    findVowels = false;
                    break;
                }

            }
            WriteLine(findVowels ? "YES" : "NO");
            ReadLine();
        }