fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace _03_CountWordsInText
  8. {
  9. class CountWords
  10. {
  11. static void Main(string[] args)
  12. {
  13. StreamReader inputText = new StreamReader("text.txt");
  14. string[] words;
  15. char[] separators = new char[] { ' ', ',', '.', '!', '?', '-', '_', '\n', '\r' };
  16. using (inputText)
  17. {
  18. string allText = inputText.ReadToEnd();
  19. words = allText.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  20. }
  21. SortedDictionary<string, int> wordCount = new SortedDictionary<string, int>();
  22. foreach (string word in words)
  23. {
  24. if (wordCount.ContainsKey(word.ToLower()))
  25. {
  26. wordCount[word.ToLower()]++;
  27. }
  28. else
  29. {
  30. wordCount.Add(word.ToLower(), 1);
  31. }
  32. }
  33. List<KeyValuePair<string, int>> result = new List<KeyValuePair<string, int>>();
  34. foreach (KeyValuePair<string,int> pair in wordCount)
  35. {
  36. result.Add(pair);
  37. }
  38. result.Sort((a, b) => { return a.Value.CompareTo(b.Value); });
  39. foreach (KeyValuePair<string,int> pair in result)
  40. {
  41. Console.WriteLine("{0} -> {1} time(s).", pair.Key, pair.Value);
  42. }
  43. }
  44. }
  45. }
  46.  
Runtime error #stdin #stdout 0.04s 41248KB
stdin
Standard input is empty
stdout
Standard output is empty