C# Program to Find the Occurrence of a Character in a String – C# Programming Example
C# C# Console

C# Program to Find the Occurrence of a Character in a String

In this tutorial, we will write a C# program to find the occurrence of a character in a String.

Program to find occurrence of a character in a string

In this program we are finding the occurrence of each character in a String. To do this we are first creating an array of size 256 (ASCII upper range), the idea here is to store the occurrence count against the ASCII value of that character. For example, the occurrence of ‘A’ would be stored in counter[65] because ASCII value of A is 65, similarly occurrences of other chars are stored in against their ASCII index values.

We are then creating an another array array to hold the characters of the given String, then we are comparing them with the characters in the String and when a match is found the count of that particular char is displayed using counter array.

Example:

Output:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: