Advertisement
Daniel_007

02. Fancy Barcodes

Apr 4th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace ConsoleApp2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int howMany = int.Parse(Console.ReadLine());
  11.             string pattern = @"@#+([A-Z][A-Za-z\d]{4,}[A-Z])@#+";
  12.             for (int i = 0; i < howMany; i++)
  13.             {
  14.                 string input = Console.ReadLine();
  15.                 Match barcode = Regex.Match(input, pattern);
  16.                 string productGroup = "00";
  17.                 bool checker = false;
  18.                 if (barcode.Success)
  19.                 {
  20.                     string product = barcode.Value;
  21.                     for (int j = 0; j < product.Length; j++)
  22.                     {
  23.                         char letter = product[j];
  24.                         if (char.IsDigit(letter))
  25.                         {
  26.                             if (!checker)
  27.                             {
  28.                                 productGroup = string.Empty;
  29.                             }
  30.                             checker = true;
  31.                             productGroup += letter;
  32.                         }
  33.                     }
  34.                     Console.WriteLine($"Product group: {productGroup}");
  35.                 }
  36.                 else
  37.                 {
  38.                     Console.WriteLine("Invalid barcode");
  39.                 }
  40.  
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement