Answers for "c# count number of occurrences in string"

C#
7

c# count number of occurrences in string

char myChar = 'x';
string myString = "xyz";

int count = myString.Count(s => s == myChar);
Posted by: Guest on February-24-2020
1

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));
    }
}
Posted by: Guest on September-01-2021

Code answers related to "c# count number of occurrences in string"

C# Answers by Framework

Browse Popular Code Answers by Language