Sha256: d9dbb5cabe1cbad183896252359b858ff214938ac08a6e8830f916de8582f7b2

Contents?: true

Size: 676 Bytes

Versions: 28

Compression:

Stored size: 676 Bytes

Contents

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public static class WordCount
{
    public static IDictionary<string, int> CountWords(string phrase)
    {
        if (phrase == null) throw new ArgumentNullException("phrase");

        var counts = new Dictionary<string, int>();
        Match match = Regex.Match(phrase.ToLower(), @"\w+'\w+|\w+");
        while(match.Success)
        {
            string word = match.Value;
            if(!counts.ContainsKey(word))
            {
                counts[word] = 0;
            }
            counts[word]++;
            match = match.NextMatch();
        }
        return counts;
    }
}

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/word-count/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/word-count/Example.cs