Advertisement
bobypenev

07. Query Mess

Jun 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace QueryMess
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             while (true)
  15.             {
  16.                 // Process input
  17.                 string[] line = Console.ReadLine().Split("&?".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  18.                 // Stop execution
  19.                 if (line[0] == "END") { break; }
  20.  
  21.                 // Store all key=values queries
  22.                 Dictionary<string, List<string>> queries = new Dictionary<string, List<string>>();
  23.  
  24.                 // Process keys=values
  25.                 for (int i = 0; i < line.Length; i++)
  26.                 {
  27.                     // Check if contains any key=value
  28.                     bool containsQuery = line[i].IndexOf('=') != -1;
  29.                     if (!containsQuery) { continue; }
  30.  
  31.                     string[] query = line[i].Split('=');
  32.                     string key = query[0].Trim();
  33.                     string value = query[1].Trim();
  34.  
  35.                     // Replace encoded spaces
  36.                     string pattern = @"(%20|\+)+";
  37.                     key = Regex.Replace(key, pattern, " ").Trim();
  38.                     value = Regex.Replace(value, pattern, " ").Trim();
  39.  
  40.                     // Add key to dictionary
  41.                     if (queries.ContainsKey(key) == false)
  42.                     {
  43.                         queries.Add(key, new List<string>());
  44.                     }
  45.                     // Add value to key
  46.                     queries[key].Add(value);
  47.                 }
  48.  
  49.                 // Print
  50.                 foreach (var query in queries)
  51.                 {
  52.                     Console.Write("{0}=[{1}]",
  53.                         query.Key,
  54.                         string.Join(", ", query.Value));
  55.                 }
  56.                 Console.WriteLine();
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement