Advertisement
desislava_topuzakova

01. Even Lines (по скелета)

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