Reputation:
I have to count how many times each word from given input text appears in it. And the thing where I'm stuck: The character casing differences should be ignored.
For example: "You are here.You you" -> the output :
are=1
here=1
You=3
What I've done:
string text = "You are here.You you";
IDictionary<string, int> wordsCount = new SortedDictionary<string, int>();
string[] words = text.Split(' ',',','.','-','!');
foreach (string word in words)
{
int count = 1;
if (wordsCount.ContainsKey(word))
count = wordsCount[word] + 1;
wordsCount[word] = count;
}
var items = from pair in wordsCount
orderby pair.Value ascending
select pair;
foreach (var p in items)
{
Console.WriteLine("{0} -> {1}", p.Key, p.Value);
}
There is a chance to make this possible without checking manually every word from the given text? For example if I have a very long paragraph to not check every word using the specific method?
Upvotes: 1
Views: 70
Reputation: 2045
Just add
for(i = 0; text[i] != '\0'; i++){
text[i] = text[i].ToLower();
}
But as text
is a string, just do :
text = text.ToLower();
Just before the string[] words = text.Split(' ',',','.','-','!');
line.
And then enjoy !
Upvotes: 1
Reputation: 1334
How about linq?
var text = "You are here.You you";
var words = text.Split(' ', ',', '.', '-', '!');
words
.GroupBy(word => word.ToLowerInvariant())
.OrderByDescending(group => group.Count())
.ToList()
.ForEach(g=> Console.WriteLine(g.Key + "=" + g.Count()));
Upvotes: 0