Advertisement
desislava_topuzakova

01. Even Lines

May 28th, 2022
737
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _01._EvenLines
  7. {
  8.     internal class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.            
  13.             string filePath = @"C:\Users\I353529\Desktop\Streams_Resources\text.txt";
  14.  
  15.             //1. прочетем четните редовете
  16.             //2. заменим символите с @
  17.             //3. reverse words
  18.  
  19.             using (StreamReader reader = new StreamReader(filePath))
  20.             {
  21.                 int counter = -1; //брой редовете
  22.                 string line = reader.ReadLine(); //четем по 1 ред от файла
  23.                
  24.                 while (line != null)
  25.                 {
  26.                     counter++;
  27.                     if (counter % 2 == 0)
  28.                     {
  29.                         //замяна
  30.                         line = Replace(line);
  31.                         //обърна в обратен ред
  32.                         line = Reverse(line);
  33.                         Console.WriteLine(line);
  34.                     }
  35.  
  36.                     line = reader.ReadLine();                  
  37.                 }
  38.             }
  39.         }
  40.  
  41.         private static string Reverse(string line)
  42.         {   //"@I was quick to judge him@ but it wasn't his fault@".Split()
  43.             //["@I", "was", "quick", "to", "judge", "him@", "but", "it", "wasn't", "his", "fault@"].Reverse()
  44.             //["fault@", "his", "wasn't", "it", "but", "him@", "judge", "to", "quick", "was", "@I"].Join(" ")
  45.             //"fault@ his wasn't it but him@ judge to quick was @I"
  46.             return string.Join(" ", line.Split().Reverse());
  47.         }
  48.  
  49.         private static string Replace(string line) //върне реда със заместени символи
  50.         {
  51.             //-I was quick to judge him, but it wasn't his fault.
  52.             //@I was quick to judge him@ but it wasn't his fault@
  53.             return line.Replace("-", "@")
  54.                  .Replace(",", "@")
  55.                  .Replace(".", "@")
  56.                  .Replace("!", "@")
  57.                  .Replace("?", "@");
  58.         }
  59.     }
  60. }
  61.  
Advertisement
RAW Paste Data Copied
Advertisement