nikiman8888

RegexExercises/CannonShot

Apr 26th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. namespace lab3.BoomingCannon
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] coordinates = Console.ReadLine().Split(' ');
  14.  
  15.             int distance = int.Parse(coordinates[0], NumberFormatInfo.InvariantInfo);
  16.             int elementsToDestroy = int.Parse(coordinates[1], NumberFormatInfo.InvariantInfo);
  17.  
  18.             string inputTarget = Console.ReadLine();
  19.            
  20.             Regex targetPatern = new Regex(@"\\<<<(?<target>[^\\<<<]+)");
  21.             MatchCollection collection = targetPatern.Matches(inputTarget);
  22.             List<string> result = new List<string>();
  23.  
  24.             foreach (Match m in collection)
  25.             {
  26.                 string target = m.Groups["target"].Value;
  27.                 string destroyedTarget = GetDestroyedTarget(distance, elementsToDestroy, target);
  28.                 result.Add(destroyedTarget);
  29.             }
  30.             Console.WriteLine(string.Join("/\\",result));
  31.            
  32.       }
  33.         static string GetDestroyedTarget(int index,int count ,string target)
  34.         {
  35.             string destroyed = "";
  36.             int countLetters = 0;
  37.            
  38.             for (int i = index; i < target.Length; i++)
  39.             {
  40.                 if (countLetters == count)
  41.                 {
  42.                     return destroyed;
  43.                 }
  44.                
  45.                 destroyed += target[i];
  46.                 countLetters++;
  47.             }
  48.             return destroyed;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment