Advertisement
dimipan80

Phone Numbers

May 26th, 2015
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. /* You are given a string, holding ASCII characters. Find all name -> phone number pairs, format them and print them in an ordered list as list items.
  2.  * The name should be at least one letter long, can contain only letters and should always start with an uppercase letter.
  3.  * The phone number should be at least two digits long, should start and end with a digit (might also start with “+”) and might contain the following symbols in it: “(“, “)”, “/”, “.”, “-”, “ “ (left and right bracket, slash, dot, dash and whitespace).
  4.  * Between a name and the phone number there might be any number of symbols, other than letters and “+”.
  5.  * Between the name -> phone number pairs there might be any number of ASCII symbols.
  6.  * The output format should be <b>[name]:</b> [phone number] (there is one space between the name and the phone number). Clear any characters other than digits and “+” from the number when printing the output.
  7.  * The command "END" denotes the end of input.
  8.  * The output should hold the resulting ordered list (on a single line), or a single paragraph, holding “No matches!” */
  9.  
  10. namespace Phone_Numbers
  11. {
  12.     using System;
  13.     using System.Collections.Generic;
  14.     using System.Linq;
  15.     using System.Text;
  16.     using System.Text.RegularExpressions;
  17.  
  18.     class PhoneNumbers
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             StringBuilder sb = new StringBuilder();
  23.             string inputLine = Console.ReadLine();
  24.             while (inputLine != "END")
  25.             {
  26.                 sb.Append(inputLine);
  27.                 inputLine = Console.ReadLine();
  28.             }
  29.  
  30.             Dictionary<string, string> contactList = new Dictionary<string, string>();
  31.             FindAllNamePhoneNumberPairsInGivenText(sb.ToString(), contactList);
  32.             PrintResults(contactList);
  33.         }
  34.  
  35.         private static void FindAllNamePhoneNumberPairsInGivenText(string text, Dictionary<string, string> contactList)
  36.         {
  37.             const string pattern =
  38. @".*?([A-Z][A-Za-z]*)[^A-Za-z+]*?(\+?\d[\s\/)(.-]*\d[\d\s\/)(.-]*)";
  39.  
  40.             MatchCollection matches = Regex.Matches(text, pattern);
  41.             foreach (Match match in matches)
  42.             {
  43.                 string keyName = match.Groups[1].Value;
  44.                 string phoneNumber = match.Groups[2].Value.Trim();
  45.                 StringBuilder sb = new StringBuilder();
  46.                 foreach (char ch in phoneNumber.Where(ch => char.IsDigit(ch) || ch == '+'))
  47.                 {
  48.                     sb.Append(ch);
  49.                 }
  50.  
  51.                 if (!contactList.ContainsKey(keyName))
  52.                 {
  53.                     contactList.Add(keyName, string.Empty);
  54.                 }
  55.  
  56.                 contactList[keyName] = sb.ToString();
  57.             }
  58.         }
  59.  
  60.         private static void PrintResults(Dictionary<string, string> contactList)
  61.         {
  62.             if (contactList.Count == 0)
  63.             {
  64.                 Console.WriteLine("<p>No matches!</p>");
  65.             }
  66.             else
  67.             {
  68.                 Console.Write("<ol>");
  69.                 foreach (var contact in contactList)
  70.                 {
  71.                     Console.Write("<li><b>{0}:</b> {1}</li>",
  72.                         contact.Key, contact.Value);
  73.                 }
  74.  
  75.                 Console.WriteLine("</ol>");
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement