0

So i have a program that asks for your birth data and i want to replace the number of the month to its matching name. For ex. if someone writes 1989.11.10 then the result would be 1989. november 10. My code:

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

    namespace hf
    {
        class Program
        {
            static void Main(string[] args)
            {
            Console.Write("Bith data: ");
            string adat = Console.ReadLine();
            string szido = Regex.Replace(adat, "[^0-9.]", "");
            Console.WriteLine("Birthday: {0}", szido);
            string[] szidotomb = new string[3];
            szidotomb = szido.Split('.');
            for (int i = 0; i < 3; i++)
                Console.WriteLine(szidotomb[i]);
            Console.ReadKey();
        }
    }
}
Detoxical
  • 49
  • 1
  • 8
  • Side note, your `szidotomb = new string[3];` is pointless because the next line you do `szidotomb = szido.Split('.');` and you throw away the `new string[3]` you just made and did nothing with and replace it with the array returned by the split function. – Scott Chamberlain Sep 24 '17 at 19:39
  • Also your code lacks of necessary checks when you get your input from a user typing at its keyboard. What if someone types 2017.99.45 ? – Steve Sep 24 '17 at 19:41

1 Answers1

1

No need to, you can use DateTime.ToLongDateString or .ToString Read your user's input from the console, then use DateTime.tryParse the string, and then print it with the aforementioned method of the class.

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

public class Program
{
    public static void Main()
    {
        DateTime dt = new DateTime();
        string userInput;
        do
        {
            Console.Write("Please enter your birth date in YYYY.MM.DD format: ");
            userInput = Console.ReadLine();
        }
        while (!DateTime.TryParseExact(userInput, "yyyy.MM.dd",
                                       CultureInfo.InvariantCulture,
                                       DateTimeStyles.None,
                                       out dt));
        Console.WriteLine("You were born on:  \"{0}\"\n", dt.ToString("yyyy.MMMM.dd"));
        Console.ReadLine();
    }
}

You can run an example here

RaidenF
  • 3,411
  • 4
  • 26
  • 42