simple solution for characters frequency in string object

后端 未结 4 999
感情败类
感情败类 2021-01-27 03:13

The task what I\'m trying to do is about showing up the frequency of every single characters from the string object, for the moment I\'ve done some part of code, just doesn\'t h

相关标签:
4条回答
  • 2021-01-27 03:22

    If you're looking to do it without Linq, then try

    var charDictionary = new Dictionary<char, int>();
    string sign = "attitude";
    foreach(char currentChar in sign)
    {
        if(charDictionary.ContainsKey(currentChar))
        { charDictionary[currentChar]++; }
        else
        { charDictionary.Add(currentChar, 1); }
    }
    
    0 讨论(0)
  • 2021-01-27 03:24

    You can easily do it using Linq like:

     string sign = "attitude";
     int count = sign.Count(x=> x== 'a');
    

    or if you want all characters count then:

     string sign = "attitude";
     var alphabetsCount = sign.GroupBy(x=> x)
                              .Select(x=>new 
                                        {
                                          Character = x.Key, 
                                          Count = x.Count()
                                        });
    

    Here is a working Example

    UPDATE:

    Without Linq you can do it with a loop and track it in a dictionary like:

    string sign = "attitude";
    Dictionary<char,int> dic = new Dictionary<char,int>();
    foreach(var alphabet in sign)
    {
        if(dic.ContainsKey(alphabet))
            dic[alphabet] = dic[alphabet] +1;
        else
            dic.Add(alphabet,1);
    }
    

    Here is Demo without Linq using Dictionary<>

    0 讨论(0)
  • 2021-01-27 03:36
    class Program
        {
            static void Main(string[] args)
            {
    
                char ch;
                Console.Write("Enter a string:");
                string str = Console.ReadLine();
                for (ch = 'A'; ch <= 'Z'; ch++)
                {
                    int count = 0;
                    for (int i = 0; i < str.Length; i++)
                    {
                        if (ch==str[i] || str[i] == (ch + 32))
                        {
                            count++;
                        }
                    }
                    if (count > 0)
                    {
                        Console.WriteLine("Char {0} having Freq of {1}", ch, count);
                    }
                }
                Console.Read();
            }
        }
    
    0 讨论(0)
  • 2021-01-27 03:39

    Here's a non Linq way to get the counts of all the unique letters.

    var characterCount= new Dictionary<char,int>();
    foreach(var c in sign)
    {
        if(characterCount.ContainsKey(c))
            characterCount[c]++;
        else
            characterCount[c] = 1;
    }
    

    Then to find out how many "a"s there are

    int aCount = 0;
    characterCount.TryGetValue('a', out aCount);
    

    Or to get all the counts

    foreach(var pair in characterCount)
    {
        Console.WriteLine("{0} - {1}", pair.Key, pair.Value);
    }
    
    0 讨论(0)
提交回复
热议问题