Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace P06.ValidUsernames
  9. {
  10.     class StartUp
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string lineOfUsers = Console.ReadLine();
  15.  
  16.             string pattern = @"\b([A-Za-z]\w{2,24})\b";
  17.  
  18.  
  19.             MatchCollection matchCollection = Regex.Matches(lineOfUsers, pattern);
  20.  
  21.  
  22.             int bestSum = 0;
  23.             int bestIndex = 0;
  24.  
  25.  
  26.             for (int i = 0; i < matchCollection.Count - 1; i++)
  27.             {
  28.                 string first = matchCollection[i].ToString();
  29.                 string second = matchCollection[i + 1].ToString();
  30.  
  31.                 int sum = SumOfConsecutiveUsernames(first, second);
  32.                 var index = i;
  33.  
  34.                 if (bestSum < sum)
  35.                 {
  36.                     bestSum = sum;
  37.                     bestIndex = index;
  38.                 }
  39.  
  40.             }
  41.  
  42.             Console.WriteLine(matchCollection[bestIndex]);
  43.             Console.WriteLine(matchCollection[bestIndex + 1]);
  44.  
  45.         }
  46.  
  47.         public static int SumOfConsecutiveUsernames(string str1, string str2)
  48.         {
  49.             int result = 0;
  50.  
  51.             for (int i = 0; i < str1.Length; i++)
  52.             {
  53.                 char letter = str1[i];
  54.                 result += letter;
  55.             }
  56.             for (int i = 0; i < str2.Length; i++)
  57.             {
  58.                 char letter = str2[i];
  59.                 result += letter;
  60.             }
  61.             return result;
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement