Get Count of Occurrence of Characters in String using C#

  1. public void CharCount()  
  2. {  
  3.     string str = Console.ReadLine();  
  4.     int length = str.Length;  
  5.     char[] charArr = new char[length];  
  6.     for (int i = 0; i < length; i++)  //Converting String to Char Array  
  7.     {  
  8.         charArr[i] = str[i];  
  9.     }  
  10.   
  11.     for (int i = 0; i < length; i++)  
  12.     {  
  13.         bool flag = false;  
  14.         for (int k = i - 1; k >= 0; k--)      //Checking whether the current char already repeated or not  
  15.         {  
  16.             if (char.ToUpper(charArr[i]) ==char.ToUpper(charArr[k]))  
  17.             {  
  18.                 flag = true;  
  19.             }  
  20.         }  
  21.         if (!flag)  
  22.         {  
  23.             int count = 1;  
  24.             for (int j = i+1; j < length; j++)  
  25.             {  
  26.                 if (char.ToUpper(charArr[i]) ==char.ToUpper(charArr[j]))  
  27.                 {  
  28.                     count++;  
  29.                 }  
  30.             }  
  31.             Console.WriteLine(char.ToLower(charArr[i]) + "=" + count);  
  32.         }  
  33.     }  
  34. }