Q:

Write a program in C# to display the characters and frequency of character from given string in LINQ Query

belongs to collection: All LINQ programs in C# with examples

0

Write a program in C# to display the characters and frequency of character from given string in LINQ Query

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
using System.Linq;
using System.Collections.Generic;
 
class LinqExercise
{
    static void Main(string[] args)
    {
        string str;
 
        Console.WriteLine("Input the string : ");
        str = Console.ReadLine();        
 
        var Frequency = from x in str
                   group x by x into y
                   select y;
        Console.Write("The frequency of the characters are :\n");
        foreach (var Arr in Frequency)
        {
            Console.WriteLine("Character " + Arr.Key + ": " + Arr.Count() + " times");
        }
        Console.ReadLine();
    }
}

Result:

Input the string : 

techstudy

The frequency of the characters are :

Character t: 2 times

Character e: 1 times

Character c: 1 times

Character h: 1 times

Character s: 1 times

Character u: 1 times

Character d: 1 times

Character y: 1 times

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a program in C# to find the uppercase words ... >>
<< Write a program in C# to display the number and fr...