c# count number of occurrences in string
char myChar = 'x';
string myString = "xyz";
int count = myString.Count(s => s == myChar);
c# count number of occurrences in string
char myChar = 'x';
string myString = "xyz";
int count = myString.Count(s => s == myChar);
count number of occurrences of each character in string C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class UserMainCode
{
public static string Frequency (string input1)
{
var d = new Dictionary<char,int>();
foreach(char i in input1)
{
if(d.ContainsKey(i))
d[i]=d[i]+1;
else
d[i]=1;
}
string str ="";
foreach (var i in d.OrderBy(key => key.Key))
{
str+=(i.Key).ToString()+i.Value.ToString();
}
return str;
}
public static void Main(string []args)
{
string s="phqgh";
Console.WriteLine(Frequency(s));
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us