C# Sharp Exercises: Find maximum occurring character in a string
C# Sharp String: Exercise-10 with Solution
Write a program in C# Sharp to find maximum occurring character in a string.
Sample Solution:-
C# Sharp Code:
using System;
public class Exercise10
{
public static void Main()
{
string str;
int[] ch_fre = new int[255];
int i = 0, max,l;
int ascii;
Console.Write("\n\nFind maximum occurring character in a string :\n");
Console.Write("--------------------------------------------------\n");
Console.Write("Input the string : ");
str = Console.ReadLine();
l=str.Length;
for(i=0; i<255; i++) //Set frequency of all characters to 0
{
ch_fre[i] = 0;
}
/* Read for frequency of each characters */
i=0;
while(i<l)
{
ascii = (int)str[i];
ch_fre[ascii] += 1;
i++;
}
// Console.Write("{0} ",(char)65);
max = 0;
for(i=0; i<255; i++)
{
if(i!=32)
{
if(ch_fre[i] > ch_fre[max])
max = i;
}
}
Console.Write("The Highest frequency of character '{0}' is appearing for number of times : {1} \n\n", (char)max, ch_fre[max]);
}
}
Sample Output:
Find maximum occurring character in a string : -------------------------------------------------- Input the string : Welcome to w3resource.com The Highest frequency of character 'e' is appearing for number of times : 4
Flowchart:
C# Sharp Practice online:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to count a total number of vowel or consonant in a string.
Next: Write a program in C# Sharp to sort a string array in ascending order.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework