Find the Number of Occurrences of a Character in a String using C#

In this blog I shall demonstrate the number of occurrences of a character in a string.

I hope this article remember our school days programming.

Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace SchoolDays

{

    class Program

    {

        static void Main(string[] args)

        {

            string input = "WWW.C-SHARPCORNER.COM";

 

            while (input.Length > 0)

            {

                Console.Write(input[0] + " : ");

                int count = 0;

                for (int j = 0; j < input.Length; j++)

                {

                    if (input[0] == input[j])

                    {

                        count++;

                    }

                }

                Console.WriteLine(count); 

                input = input.Replace(input[0].ToString(),string.Empty);               

            }

            Console.ReadLine();

        }

    }

}

Each time a character count is found then that character is removed from the string for the next iteration.

Ebook Download
View all
Learn
View all