Linq solution:
string[] words = text.ToLower()
.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
var result = words
.GroupBy(x=>x)
.Select(grp=> new
{
word = grp.Key,
count = grp.Count()
})
.OrderByDescending(o=>o.count)
.ToList();
foreach(var o in result)
{
Console.WriteLine("{0} - {1}", o.word, o.count);
}
[EDIT]
Non-Linq solution:
string filename = @"fullfilenamehere";
string[] lines = File.ReadAllLines(filename);
Dictionary<string, int> listofwords = new Dictionary<string, int>();
foreach(string line in lines)
{
string[] words = line.ToLower().Split(new char[] { '.', '?', '!', ' ', ';', ':', ',', '-' }, StringSplitOptions.RemoveEmptyEntries);
foreach(string word in words)
if(!listofwords.ContainsKey(word))
listofwords.Add(word, 1);
else
listofwords[word] += 1;
}
Console.WriteLine("Word - No. of occurencies");
foreach(KeyValuePair<string, int> kvp in listofwords)
{
Console.WriteLine("{0} - {1}", kvp.Key, kvp.Value);
}