Input = INDIA, count each char in input string

  • class Program
  • {
  •     static void Main()
  •     {
  •         // Input string
  •         string input = "INDIA";

  •         // Call the method to count occurrences
  •         var charCount = new Dictionary<char, int>();

  •         // Iterate through each character in the string
  •         foreach (var c in input)
  •         {
  •             // Check if the character is already in the dictionary
  •             if (charCount.ContainsKey(c))
  •             {
  •                 // Increment the count of the character
  •                 charCount[c]++;
  •             }
  •             else
  •             {
  •                 // Add the character to the dictionary with a count of 1
  •                 charCount[c] = 1;
  •             }
  •         }
  •         // Print the results
  •         foreach (var kvp in charCount)
  •         {
  •             Console.WriteLine($"Character: '{kvp.Key}' occurs {kvp.Value} times.");
  •         }
  •     }
  • }
  • Comments