ellapt

T14.22.WordsCountInString

Feb 3rd, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class WordsCountInString
  6. {
  7. static void Main()
  8. {
  9. Console.WriteLine("List all different words in text with info how many times each word is found\n");
  10.  
  11. string text = "The micro-processor incorporates the functions of a micro-computer's\ncentral processing unit on a single integrated circuit,\nor at most a few integrated circuits, else the micro-processor\noperates on numbers and symbols";
  12. Console.WriteLine("Original text:\n{0}\n", text);
  13. Dictionary<string, int> Words = new Dictionary<string, int>();
  14. var letters = new StringBuilder();
  15. foreach (var word in text.ToLower())
  16. {
  17. if (Char.IsLetter(word))
  18. {
  19. letters.Append(word);
  20. }
  21. else if (letters.Length > 0)
  22. {
  23. if (Words.ContainsKey(letters.ToString()))
  24. {
  25. Words[letters.ToString()]++;
  26. }
  27. else
  28. {
  29. Words.Add(letters.ToString(), 1);
  30. }
  31. letters.Clear();
  32. }
  33. }
  34.  
  35. foreach (var word in Words)
  36. {
  37. Console.WriteLine("{0,-12} - {1,3} times found", word.Key, word.Value);
  38. }
  39. Console.WriteLine();
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment