
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# Program to find number of occurrence of a character in a String
Let’s say our string is −
String s = "mynameistomhanks";
Now create a new array and pass it a new method with the string declared above. This calculates the occurrence of characters in a string.
static void calculate(String s, int[] cal) { for (int i = 0; i < s.Length; i++) cal[s[i]]++; }
Let us see the complete code.
Example
using System; class Demo { static int maxCHARS = 256; static void calculate(String s, int[] cal) { for (int i = 0; i < s.Length; i++) cal[s[i]]++; } public static void Main() { String s = "mynameistomhanks"; int[] cal = new int[maxCHARS]; calculate(s, cal); for (int i = 0; i < maxCHARS; i++) { if (cal[i] > 1) { Console.WriteLine("Character " + (char) i); Console.WriteLine("Occurrence = " + cal[i] + " times"); } if (cal[i] == 1) { Console.WriteLine("Character " + (char) i); Console.WriteLine("Occurrence = " + cal[i] + " time"); } } } }
Output
Character a Occurrence = 2 times Character e Occurrence = 1 time Character h Occurrence = 1 time Character i Occurrence = 1 time Character k Occurrence = 1 time Character m Occurrence = 3 times Character n Occurrence = 2 times Character o Occurrence = 1 time Character s Occurrence = 2 times Character t Occurrence = 1 time Character y Occurrence = 1 time
- Related Articles
- C Program to find minimum occurrence of character in a string
- C Program to find maximum occurrence of character in a string
- C program to replace all occurrence of a character in a string
- Java program to check occurrence of each character in String
- Python program to find occurrence to each character in given string
- String function to replace nth occurrence of a character in a string JavaScript
- Java program to count the occurrence of each character in a string using Hashmap
- Finding the last occurrence of a character in a String in Java
- C++ Program to Find the Frequency of a Character in a String
- Java Program to Find the Frequency of Character in a String
- Golang program to find the frequency of character in a string
- Java program to find the Frequency of a character in a given String
- Replace first occurrence of a character in Java
- Java Program to access character of a string
- How to find index of last occurrence of a substring in a string in Python?

Advertisements