Advertisement
Aborigenius

Diamonds

Aug 23rd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Diamond
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();
  14.  
  15.             int leftindex = -1;
  16.             int rightIndex = -1;
  17.             bool isFound = false;
  18.             while ((leftindex = input.IndexOf('<', leftindex +1)) > -1
  19.                 && (rightIndex = input.IndexOf('>', leftindex +1)) > -1)
  20.             {
  21.                
  22.                 string content = input.Substring(leftindex +1, rightIndex - leftindex -1);
  23.                 int carats = FindCarats(content);
  24.                 Console.WriteLine($"Found {carats} carat diamond");
  25.                 isFound = true;
  26.  
  27.             }
  28.             if (isFound == false)
  29.             {
  30.                 Console.WriteLine("Better luck next time");
  31.             }
  32.  
  33.         }
  34.         public static int FindCarats(string content)
  35.         {
  36.             int carats = 0;
  37.                 foreach (char @char in content)
  38.                 {
  39.                     if (char.IsDigit(@char))
  40.                     {
  41.                         carats += int.Parse(@char.ToString());
  42.                     }
  43.                 }
  44.             return carats;
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement