Count the number of occurrences of each letter in string

前端 未结 15 2016
别跟我提以往
别跟我提以往 2020-12-10 20:02

How can I count the number of occurrences in c of each letter (ignoring case) in the string? So that it would print out letter: # number of occurences, I have c

15条回答
  •  误落风尘
    2020-12-10 20:41

    protected void btnSave_Click(object sender, EventArgs e)
        {           
            var FullName = "stackoverflow"
    
            char[] charArray = FullName.ToLower().ToCharArray();
            Dictionary counter = new Dictionary();
            int tempVar = 0;
            foreach (var item in charArray)
            {
                if (counter.TryGetValue(item, out tempVar))
                {
                    counter[item] += 1;
                }
                else
                {
                    counter.Add(item, 1);
                }
            }
            //var numberofchars = "";
            foreach (KeyValuePair item in counter)
            {
                if (counter.Count > 0)
                {
                    //Label1.Text=split(item.
                }
                Response.Write(item.Value + " " + item.Key + "
    "); // Label1.Text=item.Value + " " + item.Key + "
    "; spnDisplay.InnerText= item.Value + " " + item.Key + "
    "; } }

提交回复
热议问题