Home » String Occurrence Count – Program

String Occurrence Count – Program

In this article, we can discuss the Occurrences count in the string

for example, Consider engineering string is given, in this string e character is repeating 3 times so we need to print the character and its occurrences.

C#

namespace StringOccurences
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "engineering";
            int length = name.Length;
            int count = 0;
            while (name.Length>count)
            {
                int occurences = 0;
                for (int i = 0; i < name.Length; i++)
                {
                    if (name[count]==name[i])
                    {
                        occurences++;
                    }
                }
                Console.WriteLine("Character name {0}" + "  " + "occurences {1}", name[count], occurences);
                count++;
            }
            Console.ReadLine();
        }
    }
}

Output:

Character name e  occurences 3
Character name n  occurences 3
Character name g  occurences 2
Character name i  occurences 2
Character name n  occurences 3
Character name e  occurences 3
Character name e  occurences 3
Character name r  occurences 1
Character name i  occurences 2
Character name n  occurences 3
Character name g  occurences 2

Need help?

Read this post again, if you have any confusion or else add your questions in Community