Advertisement
elena1234

EvenLines- with stream

Jan 16th, 2021
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace EvenLines
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             using (var reader = new StreamReader("input.txt"))
  13.             {
  14.                 using (var writer = new StreamWriter("output.txt"))
  15.                 {
  16.                     char[] replaceElements = new char[] { '-', '.', ',', '?', '!' };
  17.                     int lineCounter = 0;
  18.                     while (!reader.EndOfStream)
  19.                     {
  20.                         string line = reader.ReadLine();
  21.                         if (lineCounter % 2 == 0)
  22.                         {
  23.                             var sb = new StringBuilder(line);
  24.                             foreach (char element in replaceElements) // replace all elements with '@'
  25.                             {
  26.                                 if (sb.ToString().Contains(element))
  27.                                 {
  28.                                     sb.Replace(element, '@');
  29.                                 }
  30.                             }
  31.  
  32.                             var list = sb.ToString().Split(" ").Reverse().ToList();  // reverse all words
  33.                             sb.Clear();
  34.                             for (int i = 0; i < list.Count; i++)
  35.                             {
  36.                                 sb.Append(list[i]);
  37.                                 sb.Append(" ");
  38.                             }
  39.  
  40.                             writer.WriteLine(sb);
  41.                             list.Clear();
  42.                         }
  43.  
  44.                         lineCounter++;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement