JulianJulianov

06.TextProcessingExercise-Valid Usernames

Apr 16th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. 01.  Valid Usernames
  2. Write a program that reads user names on a single line (joined by ", ") and prints all valid usernames.
  3. A valid username is:
  4. • Has length between 3 and 16 characters
  5. • Contains only letters, numbers, hyphens and underscores
  6. Examples
  7. Input                                                         Output
  8. sh, too_long_username, !lleg@l ch@rs, jeffbutt                jeffbutt
  9.                  
  10. Jeff, john45, ab, cd, peter-ivanov, @smith                    Jeff
  11.                                                               John45
  12.                                                               peter-ivanov
  13.  
  14. using System;
  15.  
  16. namespace ExamResults
  17. {
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.               var userNames = Console.ReadLine().Split(", ");
  23.            
  24.             foreach (var userName in userNames)
  25.             {
  26.                 if (userName.Length >= 3 && userName.Length <= 16)
  27.                 {
  28.                     var counter = 0;
  29.                     foreach (var symbol in userName)
  30.                     {
  31.                         if (!(Char.IsLetterOrDigit(symbol) || symbol == '-' ||  userName.Contains('_')))
  32.                         {
  33.                             break;
  34.                         }
  35.                         else
  36.                         {
  37.                             counter++;
  38.                         }
  39.                     }
  40.                     if (counter == userName.Length)
  41.                     {
  42.                         Console.WriteLine(userName);
  43.                     }
  44.                 }
  45.             }      
  46.         }
  47.     }
  48. }
Add Comment
Please, Sign In to add comment