Advertisement
nikolayneykov

Untitled

Mar 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. class Program
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         string[] keys = Console.ReadLine().ToUpper().Split('&');
  11.         Regex pattern = new Regex(@"^[A-Z\d]{25}|[A-Z\d]{16}$");
  12.         List<string> result = new List<string>();
  13.         foreach (var k in keys)
  14.         {
  15.             if (pattern.IsMatch(k))
  16.             {
  17.                 char[] chars = k.ToCharArray();
  18.  
  19.                 for (int i = 0; i < chars.Length; i++)
  20.                 {
  21.                     if (char.IsDigit(chars[i]))
  22.                     {
  23.                         chars[i] = (char)('9' - (chars[i] - '0'));
  24.                     }
  25.                 }
  26.  
  27.                 string key = string.Join("", chars);
  28.                 if (key.Length == 25)
  29.                 {
  30.                     result.Add(key.Substring(0, 5) + "-" + key.Substring(5, 5) + "-" +
  31.                         key.Substring(10, 5) + "-" + key.Substring(15, 5) + "-" +
  32.                         key.Substring(20, 5));
  33.                 }
  34.                 else
  35.                 {
  36.                     result.Add(key.Substring(0, 4) + "-" + key.Substring(4, 4) + "-" +
  37.                        key.Substring(8, 4) + "-" + key.Substring(12, 4));
  38.                 }
  39.             }
  40.         }
  41.  
  42.         Console.WriteLine(string.Join(", ", result));
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement