Advertisement
grubcho

Mixed phones -Split(new[] {' ', ':'}, StringSplitOptions.Rem

Jul 10th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You will be given several phone entries, in the form of strings in format:
  7. //{firstElement} : {secondElement}
  8. //The first element is usually the person’s name, and the second one – his phone number. The phone number consists ONLY of digits, //while the person’s name can consist of any ASCII characters.
  9. //Sometimes the phone operator gets distracted by the Minesweeper she plays all day, and gives you first the phone, and then the name. //e.g. : 0888888888 : Pesho. You must store them correctly, even in those cases.
  10. //When you receive the command “Over”, you are to print all names you’ve stored with their phones. The names must be printed in //alphabetical order.
  11.  
  12. namespace Mixed_phones
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             SortedDictionary<string, long> phonebook = new SortedDictionary<string, long>();
  19.             string input = Console.ReadLine();
  20.  
  21.             while (input != "Over")
  22.             {
  23.                 string[] phones = input.Split(new[] {' ', ':'}, StringSplitOptions.RemoveEmptyEntries).ToArray();
  24.  
  25.                 string name = phones[0];
  26.                 string number = phones[1];
  27.                 bool isNumberNumber = number.All(char.IsDigit);
  28.                 bool isNumberName = name.All(char.IsDigit);
  29.  
  30.                 if (!isNumberName && isNumberNumber)
  31.                 {
  32.                     phonebook[name] = long.Parse(number);
  33.                 }
  34.                 else if (isNumberName && !isNumberNumber)
  35.                 {
  36.                     phonebook[number] = long.Parse(name);
  37.                 }                
  38.                 input = Console.ReadLine();
  39.             }
  40.             foreach (KeyValuePair<string, long> item in phonebook)
  41.             {
  42.                 Console.WriteLine($"{item.Key} -> {item.Value}");
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement