fork download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var txt = "moron and morons sat on moronic bench with mormons";
  10. var words = Regex.Matches(txt, @"\w+").OfType<Match>().Select(c => c.Value).ToList();
  11. var result = words.Select(c => new {Word = c, Count = words.Count(w => w.Contains(c))})
  12. .OrderByDescending(o=> o.Count).ToList();
  13.  
  14. foreach (var wc in result)
  15. {
  16. Console.WriteLine($"{wc.Word}: {wc.Count}");
  17. }
  18. }
  19. }
Success #stdin #stdout 0.03s 134720KB
stdin
Standard input is empty
stdout
on: 5
moron: 3
and: 1
morons: 1
sat: 1
moronic: 1
bench: 1
with: 1
mormons: 1