Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace _03AppearsInText
- {
- class Appearings
- {
- static string path = @"..\..\words.txt";
- static void Main(string[] args)
- {
- StreamReader reader = new StreamReader(path);
- using (reader)
- {
- string allText = reader.ReadToEnd();
- char[] separators = { ' ', ',', '-', '.', ',', '\"', '!', '?', ';', ':' };
- string[] words = allText.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- Dictionary<string, int> dict = new Dictionary<string, int>();
- foreach (string s in words)
- {
- int count = 1;
- if (dict.ContainsKey(s.Trim().ToLower()))
- {
- count = dict[s.Trim().ToLower()] + 1;
- }
- dict[s.Trim().ToLower()] = count;
- }
- PrintSortedByValue(dict);
- }
- }
- static void PrintSortedByValue(Dictionary<string, int> dict)
- {
- foreach (var item in dict.OrderBy(key => key.Value))
- {
- if (item.Value > 1)
- {
- Console.WriteLine("{0} -> {1} times", item.Key, item.Value);
- }
- else
- {
- Console.WriteLine("{0} -> {1} time", item.Key, item.Value);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement