Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _03.WordsOccurences
- {
- class WordsOcc
- {
- static void CountWords(string[] words)
- {
- Dictionary<string, int> table = new Dictionary<string, int>();
- for (int i = 0; i < words.Length; i++)
- {
- if (!table.ContainsKey(words[i]))
- {
- table.Add(words[i], 1);
- }
- else
- {
- int value = 0;
- table.TryGetValue(words[i], out value);
- value++;
- table[words[i]] = value;
- }
- }
- List<KeyValue> list = new List<KeyValue>();
- foreach (var item in table)
- {
- KeyValue kv = new KeyValue();
- kv.Count = item.Value;
- kv.Word = item.Key;
- list.Add(kv);
- }
- list.Sort();
- foreach (var item in list)
- {
- Console.WriteLine("{0} --> {1}",item.Word, item.Count );
- }
- }
- class KeyValue : IComparable
- {
- public int Count { get; set; }
- public string Word { get; set; }
- public int CompareTo(object obj)
- {
- KeyValue kv = obj as KeyValue;
- return this.Count.CompareTo(kv.Count);
- }
- }
- static void Main(string[] args)
- {
- string str = "This is the TEXT. Text, text, text – THIS TEXT! Is this the text?";
- str = str.ToLower();
- char[] delimiters = { ' ', '-', '–', '?', '!', ',', '.' };
- string[] words = str.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
- CountWords(words);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement