Counts the occurrences of each character in the string and then prints the character along with its count in c#.
Clever Cottonmouth
Posted on January 17, 2024
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string str = "hell";
Dictionary<char, int> count = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
char ch = str[i];
if (count.ContainsKey(ch))
{
count[ch]++;
}
else
{
count[ch] = 1;
}
}
foreach (var pair in count)
{
Console.WriteLine($"{pair.Key}, {pair.Value}");
}
}
}
π πͺ π
π©
Clever Cottonmouth
Posted on January 17, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
privacy Caught in the Crunch My Journey from Snacks to 2 Million Exposed Users Privacy
November 30, 2024