using System.Collections.Generic;
public static Dictionary<string, string> data = new Dictionary<string, string>()
{"key2", "value2 - $PRP:\"key1\"" },
{"key3", "$PRP:\"key5\"$PRP:\"key6\"" },
{"key4", "$PRP:\"key1\"qwerty" },
{"key7", "$PRP:\"key6\"" },
{"key8", "$PRP:\"key3\" OY $PRP:\"key5\"$PRP:\"key7\"" },
{"key9", "$PRP:\"key1\" $PRP:\"key2\" $PRP:\"key3\" $PRP:\"key4\" $PRP:\"key5\" $PRP:\"key6\" $PRP:\"key7\" $PRP:\"key8\" $PRP:\"key9\"OMG" },
{"key10", "Foo $PRP:\"key12\" Hello" },
{"key11", "Bar $PRP:\"key10\" World" },
{"key12", "Baz $PRP:\"key11\" :)" },
public static Dictionary<string, string> answers = new Dictionary<string, string>()
{"key2", "value2 - value1" },
{"key4", "value1qwerty" },
{"key8", "ATR Soft OY ATR Soft" },
{"key9", "value1 value2 - value1 ATR Soft value1qwerty ATR Soft Soft ATR Soft OY ATR Soft OMG" },
{"key10", "Foo Baz Bar World :) Hello" },
{"key11", "Bar Foo Baz :) Hello World" },
{"key12", "Baz Bar Foo Hello World :)" },
public static void Main()
foreach(var key in data.Keys)
if (answers.ContainsKey(key))
string res = ResolveKey(key, data);
Console.WriteLine(string.Format("'{0}' -> '{1}'", key, res));
if (string.Compare(answers[key], res) != 0)
Console.WriteLine(string.Format("\nFAIL! Correct would be\n'{0}' -> '{1}'", key, answers[key]));
Console.WriteLine("Congratulations! :)");
public static string ResolveKey(string key, Dictionary<string, string> data)
Stack<string> visited = new Stack<string>();
return ResolveKeyRecursive(key, data, ref visited);
private static string ResolveKeyRecursive(string key, Dictionary<string, string> data, ref Stack<string> visited)
string value = data.ContainsKey(key) ? data[key] : "";
if (visited.Contains(key))
string pointedKey = ExtractKey(value, out iStart, out iEnd);
while (pointedKey != null)
string ending = iEnd < value.Length ? value.Substring(iEnd + 1) : "";
string subResolve = ResolveKeyRecursive(pointedKey, data, ref visited);
value = value.Substring(0, iStart) + subResolve + ending;
pointedKey = ExtractKey(value, out iStart, out iEnd);
public static string ExtractKey(string value, out int iStart, out int iEnd)
if (string.IsNullOrWhiteSpace(value))
int iStartLen = "$PRP:\"".Length;
iStart = value.IndexOf("$PRP:\"");
iEnd = value.IndexOf("\"", iStart + iStartLen);
if (iStart != -1 && iEnd != -1)
return value.Substring(iStart + iStartLen, iEnd - (iStart + iStartLen));