Reverse the gender of a string: Difference between revisions

→‎{{header|Lua}}: added Lua solution
(→‎{{header|Lua}}: added Lua solution)
Line 373:
His dog belongs to her but her dog is his!
</pre>
 
=={{header|Lua}}==
Sufficient for the task as worded, but without attempting to go beyond (because several indeterminate cases exist). It does at least demonstrate an idiomatic way of doing multiple simultaneous substring substitutions.
<lang lua>function sufficient(s)
local xref = { She="He", He="She" }
return (s:gsub("(%w+)", function(s) return xref[s] or s end))
end
s = "She was a soul stripper. She took my heart!"
print(sufficient(s))
print(sufficient(sufficient(s)))
print(sufficient(sufficient(s)) == s)</lang>
{{out}}
<pre>He was a soul stripper. He took my heart!
She was a soul stripper. She took my heart!
true</pre>
 
=={{header|MiniScript}}==
Anonymous user