Advertisement
simeon3000

Diamond Problem

Aug 2nd, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1.             string input = Console.ReadLine();
  2.  
  3.             bool isDiam = false;
  4.  
  5.             int startIndex = input.IndexOf("<");
  6.             while (startIndex != - 1)
  7.             {
  8.                 int endIndex;
  9.                 string diamond = string.Empty;
  10.  
  11.                 endIndex = input.IndexOf(">", startIndex + 1);
  12.  
  13.                 if (endIndex - startIndex > 0)
  14.                 {
  15.                     diamond = input.Substring(startIndex + 1, endIndex - startIndex - 1);
  16.                 }
  17.  
  18.                 startIndex = input.IndexOf("<", startIndex + 1);
  19.  
  20.                 int sum = 0;
  21.                 for (int i = 0; i < diamond.Length; i++)
  22.                 {
  23.                     if (Char.IsDigit(diamond[i]))
  24.                     {
  25.                         sum += diamond[i] - '0';
  26.                     }
  27.                 }
  28.                 if (sum > 0)
  29.                 {
  30.                     Console.WriteLine($"Found {sum} carat diamond");
  31.                     isDiam = true;
  32.                 }
  33.             }
  34.  
  35.             if (!isDiam)
  36.             {
  37.                 Console.WriteLine("Better luck next time");
  38.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement