Advertisement
TodorMitev

SortByValue

Mar 1st, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. namespace _03AppearsInText
  9. {
  10.    
  11.     class Appearings
  12.     {
  13.         static string path = @"..\..\words.txt";
  14.         static void Main(string[] args)
  15.         {
  16.             StreamReader reader = new StreamReader(path);
  17.             using (reader)
  18.             {
  19.                 string allText = reader.ReadToEnd();
  20.                 char[] separators = { ' ', ',', '-', '.', ',', '\"', '!', '?', ';', ':' };
  21.                 string[] words = allText.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  22.                 Dictionary<string, int> dict = new Dictionary<string, int>();
  23.                 foreach (string s in words)
  24.                 {
  25.                     int count = 1;
  26.                     if (dict.ContainsKey(s.Trim().ToLower()))
  27.                     {
  28.                         count = dict[s.Trim().ToLower()] + 1;
  29.                     }
  30.                     dict[s.Trim().ToLower()] = count;
  31.                 }
  32.                 PrintSortedByValue(dict);
  33.  
  34.             }
  35.         }
  36.         static void PrintSortedByValue(Dictionary<string, int> dict)
  37.         {
  38.             foreach (var item in dict.OrderBy(key => key.Value))
  39.             {
  40.                 if (item.Value > 1)
  41.                 {
  42.                     Console.WriteLine("{0} -> {1} times", item.Key, item.Value);
  43.                 }
  44.                 else
  45.                 {
  46.                     Console.WriteLine("{0} -> {1} time", item.Key, item.Value);
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement