
- 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 get max occurred character in a String
To get the maximum occurred character in a string, loop until the length of the given string and find the occurrence.
With that, set a new array to calculate −
for (int i = 0; i < s.Length; i++) a[s[i]]++; }
The values we used above −
String s = "livelife!"; int[] a = new int[maxCHARS];
Now display the character and the occurrence −
for (int i = 0; i < maxCHARS; i++) if (a[i] > 1) { Console.WriteLine("Character " + (char) i); Console.WriteLine("Occurrence = " + a[i] + " times"); }
Let us see the complete code −
Example
using System; class Program { static int maxCHARS = 256; static void display(String s, int[] a) { for (int i = 0; i < s.Length; i++) a[s[i]]++; } public static void Main() { String s = "livelife!"; int[] a = new int[maxCHARS]; display(s, a); for (int i = 0; i < maxCHARS; i++) if (a[i] > 1) { Console.WriteLine("Character " + (char) i); Console.WriteLine("Occurrence = " + a[i] + " times"); } } }
Output
Character e Occurrence = 2 times Character i Occurrence = 2 times Character l Occurrence = 2 times
- Related Articles
- How to get max alphabetical character from the string in Python?\n
- How to print the maximum occurred character of a string in Java?
- Java Program to Get a Character From the Given String
- Print the string after the specified character has occurred given no. of times in C Program
- Program to remove string characters which have occurred before in Python
- Java Program to locate a character in a string
- Java Program to get a character located at the String's specified index
- Java Program to access character of a string
- C# Program to change a character from a string
- Java program to convert a character array to string
- Python program to find Most Frequent Character in a String
- Python program to find Least Frequent Character in a String
- Java Program to Convert Character to String
- Golang Program to Convert Character to String
- How to get a part of string after a specified character in JavaScript?

Advertisements