using System.Collections.Generic;
public static string[] wordArray = null;
public static void Main() {
Dictionary < string, int > wordsAndOccurancesReturned = GetWordsFrequencyCount("My bike and my book");
Dictionary < string, int > wordsLengthFrequencyReturned = GetWordsLengthFrequencyCount("My bike and my book");
Console.WriteLine("GetWordsFrequencyCount:");
PrintDictionary(wordsAndOccurancesReturned);
Console.WriteLine("GetWordsLengthFrequencyCount:");
PrintDictionary(wordsLengthFrequencyReturned);
public static Dictionary < string, int > GetWordsFrequencyCount(string text) {
Dictionary < string, int > wordsAndOccurances = new Dictionary < string, int > ();
wordArray = text.Split(' ');
foreach(string word in wordArray) {
string currentWord = word.ToLower();
if (!wordsAndOccurances.ContainsKey(currentWord)) {
wordsAndOccurances.Add(currentWord, CountOccurances(currentWord));
return wordsAndOccurances;
public static Dictionary < string, int > GetWordsLengthFrequencyCount(string text) {
wordArray = text.Split(' ');
Dictionary < string, int > wordsLengthFrequency = new Dictionary < string, int > ();
foreach(string word in wordArray) {
if (!wordsLengthFrequency.ContainsKey(word.Length.ToString())) {
wordsLengthFrequency.Add(word.Length.ToString(), CountOccurancesOfLenth(word));
return wordsLengthFrequency;
public static int CountOccurances(string word) {
for (int i = 0; i < wordArray.Length; i++) {
string currentWord = wordArray[i];
if (word.ToLower().Equals(currentWord.ToLower()))
public static int CountOccurancesOfLenth(string word) {
for (int i = 0; i < wordArray.Length; i++) {
string currentWord = wordArray[i];
if (word.Length == currentWord.Length)
public static void PrintDictionary(Dictionary < string, int > wordsAndOccurancesReturned) {
foreach(var entry in wordsAndOccurancesReturned) {
Console.WriteLine(string.Format(" {0}: {1}", entry.Key, entry.Value));