- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to count the occurrences of each character
Firstly, set the string −
string str = "Website"; Console.WriteLine("String: "+str);
Check for every character in the string and increment a variable that would count the number of occurrences of that character −
for (int j = 0; j < str.Length; j++) { if (str[0] == str[j]) { cal++; } }
Example
You can try to run the following code to count the occurrences of each character.
using System; public class Demo { public static void Main() { string str = "Website"; Console.WriteLine("String: "+str); while (str.Length > 0) { Console.Write(str[0] + " = "); int cal = 0; for (int j = 0; j < str.Length; j++) { if (str[0] == str[j]) { cal++; } } Console.WriteLine(cal); str = str.Replace(str[0].ToString(), string.Empty); } Console.ReadLine(); } }
Output
String: Website W = 1 e = 2 b = 1 s = 1 i = 1 t = 1
- Related Articles
- Java program to count the occurrences of each character
- How to Count Occurrences of Each Character in String in Android?
- Write a C Program to count the frequency of each character
- Count occurrences of a character in a repeated string in C++
- Pandas GroupBy – Count the occurrences of each combination
- C# program to count occurrences of a word in string
- Count occurrences of a character in string in Python
- Java Program to replace all occurrences of a given character with new character
- How to count the number of occurrences of a character in a string in JavaScript?
- Java program to count the occurrence of each character in a string using Hashmap
- Count Occurrences of Anagrams in C++
- Java program to count occurrences of a word in string
- Python program to count occurrences of an element in a tuple
- Python program to count occurrences of a word in a string
- Java Program to replace all occurrences of a given character in a string

Advertisements