TheBulgarianWolf

Extract Username and Age

Mar 22nd, 2021
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ExtractUserInfo
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Number of lines: ");
  12.             int n = int.Parse(Console.ReadLine());
  13.             for(int i = 0; i < n; i++)
  14.             {
  15.                 List<string> words = Console.ReadLine().Split(" ").ToList();
  16.                 string name = "";
  17.                 int age = 0;
  18.                 foreach (string word in words)
  19.                 {
  20.                     if (word[0] == '@' && (word.Last() == '|'))
  21.                     {
  22.                         name = word.Substring(1, word.Length - 2);
  23.                     }
  24.  
  25.                     if (word[0] == '#' && (word.Last() == '*'))
  26.                     {
  27.                         age = int.Parse(word.Substring(1, word.Length - 2));
  28.                     }
  29.  
  30.                 }
  31.                 Console.WriteLine($"{name} is {age} years old.");
  32.             }
  33.  
  34.         }
  35.     }
  36. }
  37.  
Add Comment
Please, Sign In to add comment