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.


Karunakar BhogyariPosted Aug 27, 2018, 2:02 AM EDT
You are savior of the day...
vivek kumarPosted Jun 4, 2018, 3:13 AM EDT
Very useful
Upendra Pratap ShahiPosted Jun 10, 2015, 11:44 PM EDT
nice sir..
Nitish SainiPosted Feb 12, 2015, 11:00 PM EST
How can i use each count in array.
Praveen Raveendran PillaiPosted Nov 20, 2014, 11:14 PM EST
Michael, I know that this source code is not a performace oriented one. Using various libraries available in .Net framework you can achieve the same result with very few lines of code too. My intention was to demsonstrate how we used to code such logic during school days and that too without using any of the newly added libararies from Microsoft .Net Framework.
Michael OSheaPosted Nov 2, 2014, 10:30 AM EST
This is absolutely horrid code.Note too you aren't removing a character from a string, but creating a new string for each iteration of the loop. This will play hell on garbage collection. I have posted an example of something more efficient and clear, see https://twitter.com/MichaelDOShea/status/529005571832573952
Udaya kumarPosted Oct 24, 2014, 6:50 AM EDT
Good logic
Anil Kumar MurmuPosted Jun 9, 2014, 9:45 PM EDT
good one.