Count character occurrences
Count how many times a target character occurs in a string while ignoring letter case.
Course progress
Problem 24 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
text = "Mississippi"; target = "s"
Expected output
4
What is happening?
A case-insensitive scan encounters s four times in Mississippi.
Solution strategy
Build the right mental model
Normalize the target once, normalize each character during a single pass, and increment the count for each match.
Core concepts
strings · counting
Complexity
Time O(n), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for count-character-occurrences
using System;
class Program
{
static int CountOccurrences(string text, char target)
{
char normalizedTarget = char.ToUpperInvariant(target);
int count = 0;
foreach (char symbol in text)
if (char.ToUpperInvariant(symbol) == normalizedTarget) count++;
return count;
}
static void Main()
{
Console.WriteLine(CountOccurrences("Mississippi", 's'));
}
}Knowledge check
Questions students often ask
What is the main idea behind count character occurrences?
Normalize the target once, normalize each character during a single pass, and increment the count for each match.
What is the time and space complexity of this solution?
Time O(n), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.