Advertisement
mimiiovkova

Word Count

Oct 2nd, 2020
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace Word_Count
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var dictionary = new SortedDictionary<string, int>();
  13.             string inputWords = File.ReadAllText("words.txt");
  14.             string[] words = inputWords.Split();
  15.             using var writer = new StreamWriter("output.txt");
  16.  
  17.             using (var reader = new StreamReader("text.txt"))
  18.             {
  19.                 string currentSentence = reader.ReadLine();
  20.  
  21.                 while (currentSentence != null)
  22.                 {
  23.                     foreach (var word in words)
  24.                     {
  25.                         if (currentSentence.ToLower().Contains(word))
  26.                         {
  27.  
  28.                             if (!dictionary.ContainsKey(word))
  29.                             {
  30.                                 dictionary.Add(word, 0);
  31.                                 dictionary[word]++;
  32.                             }
  33.                             else
  34.                             {
  35.                                 dictionary[word]++;
  36.                             }
  37.                         }
  38.                     }
  39.  
  40.                     currentSentence = reader.ReadLine();
  41.                 }
  42.  
  43.                 foreach (var word in dictionary.OrderByDescending(x => x.Value))
  44.                 {
  45.                     writer.WriteLine($"{word.Key} - {word.Value}");
  46.                 }
  47.             }
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement