Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The input is passed as array of query strings. For each field of the form, the query string contains a pair field=value. Within each pair, the field name and value are separated by an equals sign, '='. The series of pairs are separated by the ampersand, '&’. The question mark is used as a separator and is not part of the query string. SPACE is encoded as '+' or "%20". URL query string may contain another URL as value. Print at single line each processed string as follows: key=[value]nextkey=[another value] … etc. Multiple whitespace characters should be reduced to one inside value/key names, but there shouldn’t be any whitespaces before/after extracted keys and values. If a key already exists, the value is added with comma and whitespace after other values of the existing key in current string. */
- namespace _07.QueryMess
- {
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- class QueryMess
- {
- static void Main(string[] args)
- {
- Queue<string> linkStrings = new Queue<string>();
- StringBuilder inputLine = new StringBuilder(Console.ReadLine());
- while (inputLine.ToString() != "END")
- {
- linkStrings.Enqueue(inputLine.ToString());
- inputLine.Clear();
- inputLine.Append(Console.ReadLine());
- }
- foreach (string link in linkStrings)
- {
- GetPairsFromLinkString(link);
- }
- }
- private static void GetPairsFromLinkString(string link)
- {
- Dictionary<string, Queue<string>> pairs = new Dictionary<string, Queue<string>>();
- const string pattern = @"[^=&?\s]+=[^=&?\s]+";
- MatchCollection matches = Regex.Matches(link, pattern);
- foreach (Match match in matches)
- {
- Regex rgx = new Regex(@"(\+|%20)+");
- string pair = rgx.Replace(match.ToString(), " ");
- string[] fieldNames = pair.Split('=');
- string key = fieldNames[0].Trim();
- string value = fieldNames[1].Trim();
- if (!pairs.ContainsKey(key))
- {
- pairs[key] = new Queue<string>();
- }
- pairs[key].Enqueue(value);
- }
- PrintResultString(pairs);
- }
- private static void PrintResultString(Dictionary<string, Queue<string>> pairs)
- {
- StringBuilder result = new StringBuilder();
- foreach (var entry in pairs)
- {
- result.Append(entry.Key).Append("=[");
- result.Append(string.Join(", ", entry.Value)).Append("]");
- }
- Console.WriteLine(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement