using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace StringBuilderDemo
private static Dictionary<string, int> GetWordsFrequencyCount(string text)
var words = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
var wordPattern = new Regex(@"\w+");
foreach (Match match in wordPattern.Matches(text))
words.TryGetValue(match.Value, out currentCount);
words[match.Value] = currentCount;
private static Dictionary<int, int> GetWordsLengthFrequencyCount(string text)
var wordLengths = new Dictionary<int, int>();
var wordPattern = new Regex(@"\w+");
foreach (Match match in wordPattern.Matches(text))
wordLengths.TryGetValue(match.Value.Length, out currentCount);
wordLengths[match.Value.Length] = currentCount;
public static void Main()
string text = "My bike and my book";
var dictionaryWords = GetWordsFrequencyCount(text);
var dictionaryLengths = GetWordsLengthFrequencyCount(text);
foreach (KeyValuePair<string, int> kvp in dictionaryWords)
Console.WriteLine("\"{0}\" : {1}", kvp.Key, kvp.Value);
foreach (KeyValuePair<int, int> kvp in dictionaryLengths)
Console.WriteLine("\"{0}\" : {1}", kvp.Key, kvp.Value);