HI WELCOME TO SIRIS

C# program to count emails by domain

Leave a Comment
I have a string of emails seperated by semi colon as shown below. 
string UserInputEmails = "aa@xyz.com;cc@abc.com;bb@abc.com;dd@abc.com";


Write a c# program, that lists the total number of emails by domain. The program should give the following output.
Domain = xyz.com & Count = 1
Domain = abc.com & Count = 3


using System;using System.Linq;

namespace SamplePrograms{    class CountEmailsByDomain    {        public static void Main()        {            // User List of emails seperated by semi colon. You can have as many            // number of emails you want in this string.            string UserInputEmails = "aa@xyz.com;cc@abc.com;bb@abc.com;dd@abc.com";

            // Split the string into a string array.            string[] UserEmails = UserInputEmails.Split(';');

            // Select only the domain part of the emails into a string array, using substring() function            string[] EmailsDomain = UserEmails.Select(x => x.Substring(x.LastIndexOf("@") + 1)).ToArray();

            // Group the emails by email domain, and select the Domain and respective count            var Result = EmailsDomain.GroupBy(x => x).Select(y => new { Domain = y.Key, Count = y.Count() });

            // Finally print the domain name and the emails count            foreach (var obj in Result)            {                Console.WriteLine("Domain = {0} & Count = {1}",obj.Domain, obj.Count);            }        }    }}


0 comments:

Post a Comment

Note: only a member of this blog may post a comment.