---
title: "How to count the number of each character in a string?"  
description: "How to count the number of each character in a string?"  
author: "Revati S Misra"  
published: 2023-02-12  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/157394/how-to-count-the-number-of-each-character-in-a-string  
category: "c#"  
tags: ["c#", "java", "programs"]  
reading_time: 3 minutes  

---

# How to count the number of each character in a string?

the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) inputs a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) from which [counts](https://yourviews.mindstick.com/view/81419/it-s-what-you-don-t-say-counts) the number of each [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character)

## Replies

### Reply by Aryan Kumar

There are many ways to count the number of each character in a string in C#. Here are a few examples:

- **Using a loop:** This is the most basic way to count the number of each character in a string. You can use a for loop to iterate through the characters in the string and keep track of the number of times each character appears.

C#

```plaintext
string str = "Hello, world!";

int[] counts = new int[256]; // Create an array to store the counts

for (int i = 0; i < str.Length; i++) {
    counts[str[i]]++; // Increment the count for the current character
}

foreach (var count in counts) {
    Console.WriteLine("{0}: {1}", (char)count.Key, count.Value); // Print the count for each character
}
```

- **Using LINQ:** LINQ provides a number of methods that can be used to count the number of each character in a string. One of these methods is the `Count()` method. The `Count()` method takes a predicate as a parameter, which is a function that determines whether or not a character should be counted.

C#

```plaintext
string str = "Hello, world!";

int count = str.Count(c => c != ' '); // Count all the characters that are not spaces

Console.WriteLine("There are {0} characters in the string that are not spaces", count);
```

- **Using a dictionary:** A dictionary is a data structure that can be used to store key-value pairs. In this case, the keys would be the characters in the string and the values would be the number of times each character appears.

C#

```plaintext
string str = "Hello, world!";

Dictionary<char, int> counts = new Dictionary<char, int>(); // Create a dictionary to store the counts

foreach (char c in str) {
    int count;
    if (!counts.TryGetValue(c, out count)) {
        count = 0;
    }

    counts[c] = count + 1; // Increment the count for the current character
}

foreach (var count in counts) {
    Console.WriteLine("{0}: {1}", count.Key, count.Value); // Print the count for each character
}
```

These are just a few examples of how to count the number of each character in a string in C#. The best method to use will depend on your specific needs.

### Reply by Ashutosh Patel

The occurrence of each character in a given string can be counted using the following C# program.

```cs
using System;
using System.Collections.Generic;
namespace HelloWorld
{
 class Program
 {
   static void Main(string[] args)
   {
       Console.WriteLine("Hello World");
    Program.Countcharacter("Hello World");
   }
   internal static void Countcharacter(string str)
   {
       Dictionary<char, int> charCount = new Dictionary<char, int>();
       foreach (var character in str)
       {
           if (character != ' ')
           {
               if (!charCount.ContainsKey(character))
               {
                   charCount.Add(character, 1);
               }
               else
               {
                   charCount[character]++;
               }
           }
       }
       foreach (var character in charCount)
       {
           Console.WriteLine("{0} - {1}", character.Key, character.Value);
       }
   }
 }
}

Output:
Hello World
H - 1
e - 1
l - 3
o - 2
W - 1
r - 1
d - 1
```


---

Original Source: https://www.mindstick.com/forum/157394/how-to-count-the-number-of-each-character-in-a-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
