Find and count the no words using Generic Collections Dictionary

Find the no of word occurences 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

class CountWords
{

    public void get()
    {
        string words = "The C# is Very Cool! The Best";
        string[] splitWords=words.Split(' ');

       Dictionary dic = new Dictionary();

       foreach (string str in splitWords)
       {
           if (dic.ContainsKey(str))
           {
               dic[str] = dic[str] + 1;
           }
           else
           {
               dic.Add(str, 1);
           }
       }

       foreach (KeyValuePair var in dic)
       {
           Console.WriteLine(var.Key + ":" + var.Value);
       }
    }

    static void Main()
    {
        CountWords obj = new CountWords();
        obj.get();
        Console.ReadLine();
    }
}

0 comments :

Post a Comment