Word Counter Implementation
Posted
by kenny
on Stack Overflow
See other posts from Stack Overflow
or by kenny
Published on 2010-03-12T03:28:13Z
Indexed on
2010/03/12
3:47 UTC
Read the original article
Hit count: 157
Is there a better way than the following brute foce implementation of a c# word counting class?
UPDATED CODE: Sorry!
/// <summary>
/// A word counting class.
/// </summary>
public class WordCounter
{
Dictionary<string, int> dictTest = new Dictionary<string, int> ();
/// <summary>
/// Enters a word and returns the current number of times that word was found.
/// </summary>
/// <param name="word">The word or string found.</param>
/// <returns>Count of times Found() was called with provided word.</returns>
public int Found ( string word )
{
int count = 1;
return dictTest.TryGetValue ( word, out count ) ? ++dictTest[word] : dictTest[word] = 1;
}
}
© Stack Overflow or respective owner