Count The Number Of Characters Occurrence In A String/ Word

Way 1

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace ProgrammingInterviewFAQ.CharacterOccurenceCount  
  7. {  
  8.     class Way1   
  9.     {  
  10.         static void Main() {  
  11.             String input = "Programmer";  
  12.             string ChartoFind = "r";  
  13.             int totallengthofinput = input.Length;  
  14.             int LengthOfInputWithOutCharToFind = input.Replace(ChartoFind, "").Length;  
  15.             int resultcount = totallengthofinput - LengthOfInputWithOutCharToFind;  
  16.             Console.WriteLine("Number of times occured the character r occured is : {0}", resultcount);  
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20. }  
Way 2

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace ProgrammingInterviewFAQ.CharacterOccurenceCount   
  7. {  
  8.     class Way2   
  9.     {  
  10.         static void Main()   
  11.       {  
  12.             String input = "corner";  
  13.             char ChartoFind = 'r';  
  14.             int count = 0;  
  15.             foreach(char charfrominput in input) {  
  16.                 if (charfrominput == ChartoFind) {  
  17.                     count++;  
  18.                 }  
  19.             }  
  20.             Console.WriteLine("Number of times occured the character r occured is : {0}", count);  
  21.             Console.ReadLine();  
  22.         }  
  23.     }  
  24. }