We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Thursday, June 11, 2015

Count of each character and total character to the given input string using user defined method.


class Program
    {
        public static Dictionary<char, int> Count(string stringToCount)
        {

            Dictionary<char, int> charcount = new Dictionary<char, int>();

            foreach (var charchar in stringToCount) 
            {

                if (!charcount.ContainsKey(charchar)) 
                {

                    charcount.Add(charchar, 1); 
                } 
                else 
                {

                    charcount[charchar]++; 
                }

            }

            return charcount;

        }

        static void Main(string[] args) 
        {

            Console.WriteLine("Enter the string :");

            string str = Console.ReadLine();

            var count = Program.Count(str);


            foreach (var character in count) 
            {

                Console.WriteLine("{0}-{1}", character.Key, character.Value);

            }

            Console.WriteLine("Total number of characters: {0}", str.Count());

            Console.ReadLine();

        } 


    }

Output:

No comments: