In this example, frequency of characters in a string object is computed.
To do this, Length
property is used to find the length of a string object. Then, the for loop is iterated until the end of the string.
In each iteration, occurrence of character is checked and if found, the value of count is incremented by 1.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
static void Main(string[] args) { string str = "C# Programming Examples"; char checkCharacter = 'm'; int count = 0; for (int i = 0; i < str.Length; i++) { if (str[i] == checkCharacter) { ++count; } } Console.WriteLine("Number of {0} = {1}",checkCharacter,count); Console.ReadKey(); } |