Advertisement
yanass

Animal Sanctuary

Aug 2nd, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Animal_Sanctuary
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             string pattern = @"n:(?<name>[\W\w]+);t:(?<kind>[\W\w]+);c--(?<country>[A-Za-z\s]+)";
  13.  
  14.             int numberInputs = int.Parse(Console.ReadLine());
  15.  
  16.             int totalKG = 0;
  17.  
  18.             for (int i = 0; i < numberInputs; i++)
  19.             {
  20.                 string input = Console.ReadLine();
  21.                 Match validityCheck = Regex.Match(input, pattern);
  22.  
  23.                 if (validityCheck.Success)
  24.                 {
  25.                     string name = validityCheck.Groups["name"].Value.ToString();
  26.                     string kind = validityCheck.Groups["kind"].Value.ToString();
  27.                     string country = validityCheck.Groups["country"].Value.ToString();
  28.  
  29.                     totalKG += SumKilograms(name) + SumKilograms(kind);
  30.  
  31.                     name = GetNameKind(name);
  32.                     kind = GetNameKind(kind);
  33.  
  34.  
  35.                     Console.WriteLine($"{name} is a {kind} from {country}");
  36.                 }
  37.             }
  38.  
  39.             Console.WriteLine($"Total weight of animals: {totalKG}KG");
  40.         }
  41.  
  42.         static string GetNameKind(string text)
  43.         {
  44.             string animalData = "";
  45.             for (int i = 0; i < text.Length; i++)
  46.             {
  47.                 if (char.IsLetter(text[i]) || text[i] == ' ')
  48.                 {
  49.                     animalData += text[i];
  50.                 }
  51.             }
  52.  
  53.             return animalData;
  54.         }
  55.         static int SumKilograms(string text)
  56.         {
  57.             int sum = 0;
  58.             for (int i = 0; i < text.Length; i++)
  59.             {
  60.                 if (char.IsDigit(text[i]))
  61.                 {
  62.                     sum += text[i] - 48;
  63.                 }
  64.             }
  65.             return sum;
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement