encho253

Untitled

Nov 21st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. class WorkWithTextFiles
  8. {
  9.     static void Main()
  10.     {
  11.         string textFileOne = @"../../TextFiles/01.textFile.txt";
  12.         string textFileTwo = @"../../TextFiles/02.textFile.txt";
  13.  
  14.         string[] readFromTextFile = ReadFromTextFile(textFileOne);
  15.         List<string> sortByFirstTwoLetters = SortWordsByFirstTwoLetters(readFromTextFile);
  16.         List<string> removeEqualWords = RemoveEqualWordsInList(sortByFirstTwoLetters);
  17.         WriteToFile(textFileTwo, removeEqualWords);
  18.     }
  19.     static string[] ReadFromTextFile(string textFile)
  20.     {
  21.         StreamReader readFileOne = new StreamReader(textFile);
  22.        
  23.         using (readFileOne)
  24.         {
  25.             string textFromFile = readFileOne.ReadToEnd();
  26.             string[] wordFromFail = textFromFile.Split('\n').Select(n => Convert.ToString(n)).ToArray();
  27.  
  28.             return wordFromFail;        
  29.         }      
  30.     }
  31.     static List<string> SortWordsByFirstTwoLetters(string[] wordFromFail)
  32.     {
  33.         List<string> listWords = new List<string>();
  34.  
  35.         for (int i = 0; i < wordFromFail.Length; i++)
  36.         {
  37.             char[] words = wordFromFail[i].ToCharArray();
  38.             if (words[0] + 1 == words[1])
  39.             {
  40.                 listWords.Add(wordFromFail[i]);
  41.             }
  42.         }
  43.         return listWords;
  44.     }
  45.     static List<string> RemoveEqualWordsInList(List<string> listWords)
  46.     {
  47.         int counter = 1;
  48.  
  49.         for (int i = 0; i < listWords.Count; i++)
  50.         {
  51.             for (int j = counter; j < listWords.Count; j++)
  52.             {
  53.                 if (listWords[i] == listWords[j])
  54.                 {
  55.                     listWords.RemoveAt(j);
  56.                     j--;
  57.                 }
  58.             }
  59.             counter++;
  60.         }
  61.         return listWords;
  62.     }
  63.     static void WriteToFile(string textFileTwo,List<string> listWords)
  64.     {
  65.         listWords.Sort();
  66.         listWords.Reverse();
  67.         Console.OutputEncoding = Encoding.UTF8;
  68.         StreamWriter streamWriter = new StreamWriter(textFileTwo, false, Encoding.UTF8);
  69.         using (streamWriter)
  70.         {
  71.             foreach (var item in listWords)
  72.             {
  73.                 streamWriter.WriteLine(item);
  74.                 Console.WriteLine(item);
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment