We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Wednesday, May 22, 2019

Frequency of letters in a string in c#

public class Frequency
{
    static void Main()
    {

        char ch;
        Console.WriteLine("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}", (char)(ch + 32), count);
            }
        }
        Console.ReadLine();
    }

}

Output:


No comments: