Advertisement
dimipan80

Valid Usernames

May 13th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. /* You are given a line of usernames, between one of the following symbols: space, “/”, “\”, “(“, “)”. First you have to export all valid usernames. A valid username starts with a letter and can contain only letters, digits and “_”. It cannot be less than 3 or more than 25 symbols long. Your task is to sum the length of every 2 consecutive valid usernames and print on the console the 2 valid usernames with biggest sum of their lengths, each on a separate line.
  2.  * The usernames should start with a letter and can contain only letters, digits and “_”. The username cannot be less than 3 or more than 25 symbols long.
  3.  * Print at the console the 2 consecutive valid usernames with the biggest sum of their lengths each on a separate line. If there are 2 or more couples of usernames with the same sum of their lengths, print he left most. */
  4.  
  5. namespace _05.ValidUsernames
  6. {
  7.     using System;
  8.     using System.Text.RegularExpressions;
  9.  
  10.     class ValidUsernames
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string text = Console.ReadLine();
  15.             const string pattern = @"\b[A-Za-z]\w{2,24}\b";
  16.             MatchCollection matches = Regex.Matches(text, pattern);
  17.  
  18.             int start = 0;
  19.             int maxLength = 0;
  20.             for (int i = 0; i + 1 < matches.Count; i++)
  21.             {
  22.                 int length = matches[i].Length + matches[i + 1].Length;
  23.                 if (length > maxLength)
  24.                 {
  25.                     maxLength = length;
  26.                     start = i;
  27.                 }
  28.             }
  29.  
  30.             for (int j = start; j < start + 2; j++)
  31.             {
  32.                 Console.WriteLine(matches[j]);
  33.             }
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement