Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace QueryMess
- {
- class Program
- {
- static void Main(string[] args)
- {
- while (true)
- {
- // Process input
- string[] line = Console.ReadLine().Split("&?".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- // Stop execution
- if (line[0] == "END") { break; }
- // Store all key=values queries
- Dictionary<string, List<string>> queries = new Dictionary<string, List<string>>();
- // Process keys=values
- for (int i = 0; i < line.Length; i++)
- {
- // Check if contains any key=value
- bool containsQuery = line[i].IndexOf('=') != -1;
- if (!containsQuery) { continue; }
- string[] query = line[i].Split('=');
- string key = query[0].Trim();
- string value = query[1].Trim();
- // Replace encoded spaces
- string pattern = @"(%20|\+)+";
- key = Regex.Replace(key, pattern, " ").Trim();
- value = Regex.Replace(value, pattern, " ").Trim();
- // Add key to dictionary
- if (queries.ContainsKey(key) == false)
- {
- queries.Add(key, new List<string>());
- }
- // Add value to key
- queries[key].Add(value);
- }
- // Print
- foreach (var query in queries)
- {
- Console.Write("{0}=[{1}]",
- query.Key,
- string.Join(", ", query.Value));
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement