Mad Libs: Difference between revisions

+ D
(→‎{{header|Go}}: Improve to prompt for replacements in order of appearance)
(+ D)
Line 210:
return 0;
}</lang>
 
=={{header|D}}==
{{trans|Python}}
<lang d>import std.stdio, std.regex, std.array, std.algorithm,
std.string, std.range;
 
void main() {
auto story = "<name> went for a walk in the park. <he or she>
found a <noun>. <name> decided to take it home.";
writeln("The story template is:\n", story);
writeln("\nInput a comma-separated list of words "
"to replace the following items:");
auto re = regex("<[^>]+>", "g");
auto fields = story.match(re).map!q{a.hit}().array().sort().uniq();
writef(" %s: ", fields.join(","));
const values = std.string.split(stdin.readln().strip(), ",");
foreach (f, v; zip(fields, values))
story = story.replace(f, v);
writeln("\nThe story becomes:\n", story);
}</lang>
{{out}}
<pre>The story template is:
<name> went for a walk in the park. <he or she>
found a <noun>. <name> decided to take it home.
 
Input a comma-separated list of words to replace the following items:
<he or she>,<name>,<noun>: She,Monica,cockerel
 
The story becomes:
Monica went for a walk in the park. She
found a cockerel. Monica decided to take it home.</pre>
 
=={{header|Go}}==