
- 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
Find frequency of each word in a string in C#
To find the frequency of each word in a string, you can try to run the following 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 = "Football!"; 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"); } } }
Output
Character l Occurrence = 2 times Character o Occurrence = 2 times
Above, we have set a string and an integer array. This helps us in getting the repeated values. The method calculates −
static void calculate(String s, int[] cal) { for (int i = 0; i < s.Length; i++) cal[s[i]]++; }
The value are then printed that shows the duplicate elements as well −
for (int i = 0; i < maxCHARS; i++) if(cal[i] > 1) { Console.WriteLine("Character "+(char)i); Console.WriteLine("Occurrence = " + cal[i] + " times"); }
- Related Articles
- Find frequency of each word in a string in Java
- Find frequency of each word in a string in Python
- Python – Word Frequency in a String
- C program to find frequency of each digit in a string
- Frequency of each character in String in Python
- Print first letter of each word in a string in C#
- Getting first letter of each word in a String using regex in Java
- Print first letter of each word in a string using C# regex
- Golang program to capitalize first character of each word in a string
- Write a java program to reverse each word in string?
- Write a java program to tOGGLE each word in string?
- Extracting each word from a string using Regex in Java
- How to print the first character of each word in a String in Java?
- Java Program to Capitalize the first character of each word in a String
- Write a Java program to capitalize each word in the string?

Advertisements