Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Linq;
- class words
- {
- public string Text { set; get; }
- public int Count { set; get; }
- }
- class RemoveWords
- {
- static void Main()
- {
- try
- {
- StreamReader readerWord = new StreamReader("..//..//words.txt");
- StreamReader readerInput = new StreamReader("..//..//test.txt");
- StreamWriter writer = new StreamWriter("..//..//result.txt");
- List<words> wordList = new List<words>();
- using (readerWord)
- {
- string line = readerWord.ReadLine();
- while (line != null)
- {
- words word = new words();
- word.Count = 0;
- word.Text = line;
- wordList.Add(word);
- line = readerWord.ReadLine();
- }
- }
- using (readerInput)
- {
- string line = readerInput.ReadLine();
- while (line != null)
- {
- for (int i = 0; i < wordList.Count; i++)
- {
- int count = Regex.Matches(line, "\\b" + wordList[i].Text + "\\b").Count;
- wordList[i].Count += count;
- }
- line = readerInput.ReadLine();
- }
- }
- List<words> ordered = wordList.OrderByDescending(a => a.Count).ToList();
- using (writer)
- {
- foreach (var word in ordered)
- {
- writer.WriteLine(word.Text);
- }
- }
- }
- catch (DirectoryNotFoundException)
- {
- Console.WriteLine("Directory not found");
- }
- catch (FileNotFoundException)
- {
- Console.WriteLine("File not found");
- }
- catch (IOException)
- {
- Console.WriteLine("IO Exception");
- }
- catch (UnauthorizedAccessException)
- {
- Console.WriteLine("Unauthorized Access");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement