Jump to content

Reverse the gender of a string: Difference between revisions

Added Wren
m (Fix Perl 6 -> Raku in comments)
(Added Wren)
Line 1,673:
blood in her veins-royal stuff; though sadly vitiated, I fear,
by the cannibal propensity she nourished in her untutored youth.
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-str}}
<lang ecmascript>import "/str" for Char
 
var swaps = {
"She": "He", "she": "he", "Her": "His", "her": "his", "hers": "his", "He": "She",
"he": "she", "His": "Her", "his": "her", "him": "her"
}
 
var reverseGender = Fn.new { |sentence|
var newWords = []
for (word in sentence.split(" ")) {
var s = swaps[word]
if (s) {
newWords.add(s)
} else if (Char.isPunctuation(word[-1]) && (s = swaps[word[0..-2]])) {
newWords.add(s + word[-1])
} else {
newWords.add(word)
}
}
return newWords.join(" ")
}
 
var sentences = [
"She was a soul stripper. She took his heart!",
"He was a soul stripper. He took her heart!",
"She wants what's hers, he wants her and she wants him!",
"Her dog belongs to him but his dog is hers!"
]
 
for (sentence in sentences) System.print(reverseGender.call(sentence))</lang>
 
{{out}}
<pre>
He was a soul stripper. He took her heart!
She was a soul stripper. She took his heart!
He wants what's his, she wants his and he wants her!
His dog belongs to her but her dog is his!
</pre>
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.