Advertisement
stanevplamen

02.08.22.WordsCount

Aug 23rd, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. class WordsCountStandard
  7. {
  8.     static Dictionary<string, int> WordsCount(string[] words)
  9.     {
  10.         Dictionary<string, int> wordsCountDict = new Dictionary<string, int>();
  11.  
  12.         for (int i = 0; i < words.Length; i++)
  13.         {
  14.             if (wordsCountDict.ContainsKey(words[i]))
  15.             {
  16.                 wordsCountDict[words[i]]++;
  17.             }
  18.             else
  19.             {
  20.                 wordsCountDict.Add(words[i], 1);
  21.             }
  22.         }
  23.         return wordsCountDict;
  24.     }
  25.  
  26.     static void Main()
  27.     {
  28.         string sentence = "C# is not C++, not PHP and not Delphi!,C# is not C++,C# is not C++.....";
  29.         string[] words = sentence.Split(new char[] { ',', ' ', '.', '!' }, StringSplitOptions.RemoveEmptyEntries);
  30.  
  31.         Dictionary<string, int> dotNetWords = WordsCount(words);
  32.  
  33.         foreach (KeyValuePair<string, int> kvp in dotNetWords)
  34.         {
  35.             string answer = string.Format("{0} - {1}", kvp.Key, kvp.Value);
  36.             Console.WriteLine(answer);
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement