w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: LINQ : Display the characters and frequency of character from giving string

C# Sharp LINQ : Exercise-5 with Solution

Write a program in C# Sharp to display the characters and frequency of character from giving string.

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;
using System.Collections.Generic;
 
class LinqExercise5
{
    static void Main(string[] args)
    {
      string str; 

            Console.Write("\nLINQ : Display the characters and frequency of character from giving string : "); 
            Console.Write("\n----------------------------------------------------------------------------\n");	
            Console.Write("Input the string : ");
            str= Console.ReadLine();
            Console.Write("\n");
        
            var FreQ = from x in str  
                    group x by x into y  
                    select y;  
                Console.Write("The frequency of the characters are :\n");
                foreach(var ArrEle in FreQ)  
                    {  
                    Console.WriteLine("Character "+ArrEle.Key + ": " + ArrEle.Count()+" times");  
                    }  
    }
}
 

Sample Output:

LINQ : Display the characters and frequency of character from giving string :                                                                 
----------------------------------------------------------------------------                                                                  
Input the string : w3resource                                          
                                                                       
The frequency of the characters are :                                  
Character w: 1 times                                                   
Character 3: 1 times                                                   
Character r: 2 times                                                   
Character e: 2 times                                                   
Character s: 1 times                                                   
Character o: 1 times                                                   
Character u: 1 times                                                   
Character c: 1 times

Flowchart:

Flowchart: LINQ : Display the characters and frequency of character from giving string

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to display the number and frequency of number from given array.
Next: Write a program in C# Sharp to display the name of the days of a week.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming