How to find the count of numbers,letters and special character from the given string C#

This is a common interview question. here I am going to solve it using C# programming language.

 




       

 static class Program
    {
        static void Main(string[] args)
        {
            FindCount("abc2D$%@");
        }

        public static void FindCount(string input)
        {
            int num_count = 0;
            int letter_count = 0;
            int symbol_Cont = 0;

            foreach (char item in input)
            {
                if (char.IsNumber(item))
                {
                    num_count = num_count + 1;

                }
                else if (char.IsLetter(item))
                {
                    letter_count++;
                }
                else
                {
                    symbol_Cont++;
                }
            }

            Console.WriteLine("Num Count: {0},letter Count: {1} symbol Count: {2} ",
             num_count, letter_count, symbol_Cont);
            Console.ReadLine();
        }
    } 


0 comments: