simple solution for characters frequency in string object

后端 未结 4 1006
感情败类
感情败类 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条回答
  •  -上瘾入骨i
    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();
            }
        }
    

提交回复
热议问题