Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- namespace _3._Word_Count
- {
- class Program
- {
- static async Task Main(string[] args)
- {
- Dictionary<string, int> allCoincidences = new Dictionary<string, int>();
- using (StreamReader streamReaderWords = new StreamReader("words.txt"))
- {
- string currentWord = await streamReaderWords.ReadLineAsync();
- using (StreamReader streamReaderText = new StreamReader("text.txt"))
- {
- string currentString = await streamReaderText.ReadLineAsync();
- using (StreamWriter streamWriter = new StreamWriter("Output.txt"))
- {
- while (currentString != null)
- {
- string[] arrayWords =
- currentWord.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
- string[] arrayText = currentString.Split(new char[] {' ','-','.','?','!',','},StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- for (int word = 0; word < arrayWords.Length; word++)
- {
- string currentWordToCompare = arrayWords[word];
- for (int wordFromString = 0; wordFromString < arrayText.Length; wordFromString++)
- {
- string currentWordFromString = arrayText[wordFromString].ToLower();
- if (currentWordToCompare == currentWordFromString)
- {
- if (allCoincidences.ContainsKey(currentWordToCompare) == false)
- {
- allCoincidences.Add(currentWordToCompare, 1);
- }
- else
- {
- allCoincidences[currentWordToCompare]++;
- }
- }
- }
- }
- currentString = await streamReaderText.ReadLineAsync();
- }
- foreach (KeyValuePair<string, int> keyValuePair in allCoincidences.OrderByDescending(x => x.Value))
- {
- string textToWrite = $"{keyValuePair.Key} - {keyValuePair.Value}";
- await streamWriter.WriteLineAsync(textToWrite);
- }
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment