Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- //Write a program that reads a list of words from a file words.txt
- //and finds how many times each of the words is contained in another file test.txt.
- //The result should be written in the file result.txt and
- //the words should be sorted by the number of their occurrences in descending order.
- //Handle all possible exceptions in your methods.
- class CountContainedWords
- {
- private static void CountWord(IDictionary<string,int> dictionary, string line)
- {
- string checkWord = "";
- for (int i = 0; i < line.Length; i++)
- {
- while (line[i] != ' ' && line[i] != ',' && line[i] != '.')
- {
- checkWord += line[i];
- i++;
- }
- foreach (string word in dictionary.Keys)
- {
- if (checkWord == word)
- {
- dictionary[word]++;
- break;
- }
- }
- checkWord = "";
- }
- }
- static void Main(string[] args)
- {
- StreamReader text = new StreamReader(@"..\..\test.txt");
- StreamReader words = new StreamReader(@"..\..\words.txt");
- StreamWriter result = new StreamWriter(@"..\..\result.txt");
- IDictionary<string, int> dictionary = new SortedDictionary<string, int>();
- using (words)
- {
- string line;
- while ((line = words.ReadLine()) != null)
- {
- dictionary.Add(line, 0);
- }
- }
- using (text)
- {
- using (result)
- {
- string line = words.ReadLine();
- while (line != null)
- {
- CountWord(dictionary, line);
- line = words.ReadLine();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement