Reverse the gender of a string: Difference between revisions

m
(→‎{{header|J}}: Added a fully optimised Haskell version, with minimal space and time complexity, and fairly compact source code.)
m (→‎{{header|Wren}}: Minor tidy)
 
(28 intermediate revisions by 13 users not shown)
Line 1:
{{draft task|String manipulation}}
The task is to create a function that reverses the gender of the text of a string. The function should take one arguments being a string to undergo the sex change. The returned string should contain this initial string, with all references to gender switched.
 
The task is to create a function that reverses the gender of the text of a string.
<lang pseudocode>print rev_gender("She was a soul stripper. She took my heart!")
 
He was a soul stripper. He took my heart!</lang>
The function should take one arguments being a string to undergo the gender change.
 
The returned string should contain this initial string, with all references to be gender switched.
 
<syntaxhighlight lang="pseudocode">print rev_gender("She was a soul stripper. She took my heart!")
He was a soul stripper. He took my heart!</syntaxhighlight>
 
 
{{Template:Strings}}
<br><br>
 
=={{header|11l}}==
{{trans|Kotlin}}
 
<syntaxhighlight lang="11l">F reverse_gender(=s)
V words = [‘She’, ‘she’, ‘Her’, ‘her’, ‘hers’, ‘He’, ‘he’, ‘His’, ‘his’, ‘him’]
V repls = [‘He_’, ‘he_’, ‘His_’, ‘his_’, ‘his_’, ‘She_’, ‘she_’, ‘Her_’, ‘her_’, ‘her_’]
 
L(word, repl) zip(words, repls)
s = s.replace(re:(‘\b’word‘\b’), repl)
 
R s.replace(‘_’, ‘’)
 
print(reverse_gender(‘She was a soul stripper. She took his heart!’))
print(reverse_gender(‘He was a soul stripper. He took her heart!’))
print(reverse_gender(‘She wants what's hers, he wants her and she wants him!’))
print(reverse_gender(‘Her dog belongs to him but his dog is hers!’))</syntaxhighlight>
 
{{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>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE PTR="CARD"
DEFINE COUNT="10"
PTR ARRAY words(COUNT)
PTR ARRAY repls(COUNT)
 
PROC Init()
words(0)="She" repls(5)=words(0)
words(1)="she" repls(6)=words(1)
words(2)="Her" repls(7)=words(2)
words(3)="her" repls(8)=words(3)
words(4)="hers" repls(9)=words(4)
words(5)="He" repls(0)=words(5)
words(6)="he" repls(1)=words(6)
words(7)="His" repls(2)=words(7)
words(8)="his" repls(3)=words(8)
words(9)="him" repls(4)=words(9)
RETURN
 
PROC Append(CHAR ARRAY text,suffix)
BYTE POINTER srcPtr,dstPtr
BYTE len
 
len=suffix(0)
IF text(0)+len>255 THEN
len=255-text(0)
FI
IF len THEN
srcPtr=suffix+1
dstPtr=text+text(0)+1
MoveBlock(dstPtr,srcPtr,len)
text(0)==+suffix(0)
FI
RETURN
 
BYTE FUNC IsAlpha(CHAR c)
IF c>='A AND c<='Z OR c>='a AND c<='z THEN
RETURN (1)
FI
RETURN (0)
 
BYTE FUNC NextItem(CHAR ARRAY text BYTE start,word CHAR ARRAY res)
BYTE i
 
i=start
WHILE i<=text(0) AND IsAlpha(text(i))=word
DO i==+1 OD
SCopyS(res,text,start,i-1)
RETURN (i)
 
BYTE FUNC WordIndex(CHAR ARRAY text)
BYTE i
 
FOR i=0 TO COUNT-1
DO
IF SCompare(text,words(i))=0 THEN
RETURN (i)
FI
OD
RETURN (255)
 
PROC ReverseGender(CHAR ARRAY in,out)
CHAR ARRAY s(256)
BYTE start,word,index
 
out(0)=0
start=1 word=1
WHILE start<=in(0)
DO
start=NextItem(in,start,word,s)
index=WordIndex(s)
word=1-word
IF index=255 THEN
Append(out,s)
ELSE
Append(out,repls(index))
FI
OD
RETURN
 
PROC Test(CHAR ARRAY in)
CHAR ARRAY res(256)
 
ReverseGender(in,res)
PrintF("Input: ""%S""%E%E",in)
PrintF("Output: ""%S""%E%E%E",res)
RETURN
 
PROC Main()
Init()
Test("She was a soul stripper. She took his heart!")
Test("She wants what's hers, he wants her and she wants him!")
Test("She, she, Her, her, hers, He, he, His, his, him")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Reverse_the_gender_of_a_string.png Screenshot from Atari 8-bit computer]
<pre>
Input: "She was a soul stripper. She took his heart!"
 
Output: "He was a soul stripper. He took her heart!"
 
 
Input: "She wants what's hers, he wants her and she wants him!"
 
Output: "He wants what's him, she wants his and he wants hers!"
 
 
Input: "She, she, Her, her, hers, He, he, His, his, him"
 
Output: "He, he, His, his, him, She, she, Her, her, hers"
</pre>
 
=={{header|Arturo}}==
 
{{trans|Kotlin}}
 
<syntaxhighlight lang="rebol">reverseGender: function [str][
ret: new str
entries: ["She" "she" "Her" "her" "hers" "He" "he" "His" "his" "him"]
repls: ["He_" "he_" "His_" "his_" "his_" "She_" "she_" "Her_" "her_" "her_"]
loop.with:'i entries 'entry ->
replace 'ret to :regex ~{\b|entry|\b} repls\[i]
return replace ret "_" ""
]
print reverseGender "She was a soul stripper. She took his heart!"
print reverseGender "He was a soul stripper. He took her heart!"
print reverseGender "She wants what's hers, he wants her and she wants him!"
print reverseGender "Her dog belongs to him but his dog is hers!"</syntaxhighlight>
 
{{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>
 
=={{header|FreeBASIC}}==
Line 9 ⟶ 182:
 
To avoid swapping words which have already been swapped, thereby nullifying the original swap, I've appended an underscore to each replacement word and then removed all the underscores when all swaps have been made. This assumes, of course, that the text didn't include any underscores to start with.
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function isWordChar(s As String) As Boolean
Line 55 ⟶ 228:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 66 ⟶ 239:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 87 ⟶ 260:
fmt.Println(t)
fmt.Println(reverseGender(t))
}</langsyntaxhighlight>
 
{{out}}
Line 97 ⟶ 270:
=={{header|Haskell}}==
 
We can optimise the time and space complexity of this computation by careful selection of the source and target languages (not specified in the task description, although the example appears to be drawn from some kind of Anglo -Saxon dialect, which seems a bit sub-optimal for these purposes).
 
Sino-Tibetan dialects generally work well here. If we choose any standard transcription of Modern Standard Chinese (such as Pinyin or IPA) or more or less any written sample example of pre 19c literary Chinese, we can reduce the entire computation down to a very pleasing intersection of code golf with fully optimized space and time performance with reasonably clear and succinct code:
 
 
<lang haskell>id</lang>
 
{{works with|汉语拼音}}
{{works with|文言文}}
<syntaxhighlight lang="haskell">id</syntaxhighlight>
 
=={{header|J}}==
Line 117 ⟶ 290:
So, for now, we limit ourselves to the simple case specified in the task example, and do not even do all that great of a job there, either:
 
<langsyntaxhighlight Jlang="j">cheaptrick=: rplc&(;:'She He He She')</langsyntaxhighlight>
 
And, the task example:
 
<langsyntaxhighlight Jlang="j"> cheaptrick 'She was a soul stripper. She took my heart!'
He was a soul stripper. He took my heart!
cheaptrick cheaptrick 'She was a soul stripper. She took my heart!'
She was a soul stripper. She took my heart!</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|J}}
<langsyntaxhighlight lang="java">public class ReallyLameTranslationOfJ {
 
public static void main(String[] args) {
Line 143 ⟶ 316:
return s;
}
}</langsyntaxhighlight>
 
<pre>He was a soul stripper. He took my heart!
She was a soul stripper. She took my heart!</pre>
 
=={{header|jq}}==
''Adapted from [[#Wren|Wren]]''
{{works with|jq}}
'''Also works with both jaq and gojq, the Rust and Go implementations of jq'''
 
In this entry, we use the Unicode character classes for "open", "close",
and "other" punctuation, but show the test for a small set of specific
punctuation characters.
 
<syntaxhighlight lang="jq">
def swaps: {
"She": "He", "she": "he", "Her": "His", "her": "his", "hers": "his", "He": "She",
"he": "she", "His": "Her", "his": "her", "him": "her"
};
 
def isPunctuation:
type == "string" and
length == 1 and
# test("[!\"#%&'()*,-./:;?@\\[\\]\\\\_{}¡§«¶·»¿]")
# open, close, other
test("\\p{Ps}|\\p{Pe}|\\p{Po}|")
;
def reverseGender:
reduce splits(" *") as $word ([];
swaps[$word] as $s
| if $s then . + [$s]
elif ($word[-1:] | isPunctuation)
then swaps[$word[:-1]] as $s
| if $s then . + [$s + $word[-1:]]
else . + [$word]
end
else . + [$word]
end)
| join(" ");
 
def 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!"
];
 
sentences[]
| reverseGender
</syntaxhighlight>
{{Output}}
As for [[#Wren|Wren]].
 
=={{header|Julia}}==
{{trans|Kotlin}}
<syntaxhighlight lang="julia">module ReverseGender
 
const MARKER = "\0"
 
const words = "^" .* ["She", "she", "Her", "her", "hers", "He", "he", "His", "his", "him"] .* "\$" .|> Regex
const repls = ["He", "he", "His", "his" ,"his", "She", "she", "Her", "her", "her"] .* MARKER
 
function reverse(s::AbstractString)
for (w, r) in zip(words, repls)
s = replace(s, w => r)
end
return replace(s, MARKER => "")
end
 
end # module ReverseGender
 
@show ReverseGender.reverse("She was a soul stripper. She took his heart!")
@show ReverseGender.reverse("He was a soul stripper. He took her heart!")
@show ReverseGender.reverse("She wants what's hers, he wants her and she wants him!")
@show ReverseGender.reverse("Her dog belongs to him but his dog is hers!")</syntaxhighlight>
 
=={{header|Kotlin}}==
This program uses a similar approach to the FreeBASIC entry:
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun reverseGender(s: String): String {
Line 168 ⟶ 413:
println(reverseGender("She wants what's hers, he wants her and she wants him!"))
println(reverseGender("Her dog belongs to him but his dog is hers!"))
}</langsyntaxhighlight>
 
{{out}}
Line 177 ⟶ 422:
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.
<syntaxhighlight 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)</syntaxhighlight>
{{out}}
<pre>He was a soul stripper. He took my heart!
She was a soul stripper. She took my heart!
true</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">cap = function(w) // (capitalize a word)
return w[0].upper + w[1:]
end function
 
trans = {"she":"he", "her":"his", "hers":"his"}
trans = trans + {"he":"she", "his":"her", "him":"her"}
 
for k in trans.indexes
trans[cap(k)] = cap(trans[k])
end for
 
reverseGender = function(s)
s = s.replace(".", " .").replace(",", " ,").replace("?", " ?").replace("!", " !")
words = s.split
for i in words.indexes
if trans.hasIndex(words[i]) then words[i] = trans[words[i]]
end for
s = words.join
s = s.replace(" .", ".").replace(" ,", ",").replace(" ?", "?").replace(" !", "!")
return s
end function
 
test = function(s)
print "BEFORE: " + s
print "AFTER: " + reverseGender(s)
print
end function
 
test "She was a soul stripper. She took his heart!"
test "He was a soul stripper. He took her heart!"
test "She wants what's hers, he wants her and she wants him!"
test "Her dog belongs to him but his dog is hers!"</syntaxhighlight>
 
{{out}}
<pre>BEFORE: She was a soul stripper. She took his heart!
AFTER: He was a soul stripper. He took her heart!
 
BEFORE: He was a soul stripper. He took her heart!
AFTER: She was a soul stripper. She took his heart!
 
BEFORE: She wants what's hers, he wants her and she wants him!
AFTER: He wants what's his, she wants his and he wants her!
 
BEFORE: Her dog belongs to him but his dog is hers!
AFTER: His dog belongs to her but her dog is his!</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">import re, strutils
 
const
Words = ["She", "she", "Her", "her", "hers", "He", "he", "His", "his", "him"]
Repls = ["He_", "he_", "His_", "his_" ,"his_", "She_", "she_", "Her_", "her_", "her_"]
 
func reverseGender(s: string): string =
result = s
for i, word in Words:
let r = re(r"\b" & word & r"\b")
result = result.replace(r, Repls[i])
result = result.replace("_", "")
 
echo reverseGender("She was a soul stripper. She took his heart!")
echo reverseGender("He was a soul stripper. He took her heart!")
echo reverseGender("She wants what's hers, he wants her and she wants him!")
echo reverseGender("Her dog belongs to him but his dog is hers!")</syntaxhighlight>
 
{{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>
 
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang="objeck">class ReallyLameTranslationOfJ {
function : Main(args : String[]) ~ Nil {
s := "She was a soul stripper. She took my heart!";
Line 198 ⟶ 531:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 208 ⟶ 541:
=={{header|Perl}}==
A minimal implementation, using a hash to manage swaps. But this approach breaks down if, for instance, 'him' were to replace 'me', as 'her' can't be used to map to both 'his' and 'him'.
<langsyntaxhighlight lang="perl">my %swaps = (
'she' => 'he',
'his' => 'her',
Line 225 ⟶ 558:
$original = qq{She was this soul sherpa. She took his heart! They say she's going to put me on a shelf.\n};
print $swapped = gender_swap($original);
print $reverted = gender_swap($swapped);</langsyntaxhighlight>
{{out}}
<pre>He was this soul sherpa. He took her heart! They say he's going to put me on a shelf.
She was this soul sherpa. She took his heart! They say she's going to put me on a shelf.</pre>
 
=={{header|Perl 6}}==
Mechanically, this task is trivial. Perl 6 provides several flexible and powerful methods to wrangle text. Linguistically, this task is impossible (and laughable). Mappings are non-regular and in many cases, non-deterministic without semantic analysis of the content and context, which is '''WAY''' beyond what anyone is going to invest in a Rosettacode task. Whatever.
 
For extremely limited circumstances such as this example, this should suffice. Notice case matching and replication. Handles contractions, but ignores embedded matching text.
 
<lang perl6>say S:g:ii/«'she'»/he/ given "She was a soul stripper. She took my heart! They say she's going to put me on a shelf.";</lang>
{{out}}
<pre>He was a soul stripper. He took my heart! They say he's going to put me on a shelf.</pre>
 
=={{header|Phix}}==
Oh well, I tried... There are a couple of mildly interesting points though:<br>
words is a pair-list, ie "she","he" maps both ways, with first-upper-case handled too, and<br>
<lang Phix>constant words = {"she","he","his","her","him","her","hers","his"}
replacing the words right->left means no need to fiddle with indexes when lengths differ.
 
<!--<syntaxhighlight lang="phix">(phixonline)-->
function reverse_gender(string s)
<span style="color: #008080;">constant</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"she"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"he"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"his"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"her"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"him"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"her"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"hers"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"his"</span><span style="color: #0000FF;">}</span>
integer wordend, ch
bool inword = false, wordch
<span style="color: #008080;">function</span> <span style="color: #000000;">reverse_gender</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for i=length(s) to 0 by -1 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">wordend</span>
ch = iff(i=0?' ':s[i])
<span style="color: #004080;">bool</span> <span style="color: #000000;">inword</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">wordch</span>
wordch = not find(ch," .,'!\n")
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">0</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
if inword then
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">:</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
if not wordch then
<span style="color: #000000;">wordch</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" .,'!\n"</span><span style="color: #0000FF;">)</span>
string this = lower(s[i+1..wordend])
<span style="color: #008080;">if</span> <span style="color: #000000;">inword</span> <span style="color: #008080;">then</span>
integer k = find(this,words)
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">wordch</span> <span style="color: #008080;">then</span>
if k then
<span style="color: #004080;">string</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">wordend</span><span style="color: #0000FF;">])</span>
string rep = words[iff(mod(k,2)?k+1:k-1)]
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)</span>
if s[i+1]!=words[k][1] then rep[1] = upper(rep[1]) end if
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
s[i+1..wordend] = rep
<span style="color: #004080;">string</span> <span style="color: #000000;">rep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)]</span>
end if
<span style="color: #000080;font-style:italic;">-- if s[i+2..wordend]=rep[2..$] then -- might be wanted here
inword = false
-- -- (either skipping completely or all upper-&gt;all upper)</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rep</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
else
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">wordend</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span>
if wordch then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
inword = true
<span style="color: #000000;">inword</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
wordend = i
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">elsif</span> <span style="color: #000000;">wordch</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">inword</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
end for
<span style="color: #000000;">wordend</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
return s
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
constant tests = {"She was a soul stripper. She took my heart!\n",
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
"Her dog belongs to him but his dog is hers!\n"} -- ha ha!
 
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"She was a soul stripper. She took my heart!\n"</span><span style="color: #0000FF;">,</span>
for i=1 to length(tests) do
<span style="color: #008000;">"Her dog belongs to him but his dog is hers!\n"</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- ha ha!</span>
string ti = tests[i],
r = reverse_gender(ti),
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
rr = reverse_gender(r)
<span style="color: #004080;">string</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
puts(1,ti&r&rr&"\n")
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">reverse_gender</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span>
end for</lang>
<span style="color: #000000;">rr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">reverse_gender</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">&</span><span style="color: #000000;">r</span><span style="color: #0000FF;">&</span><span style="color: #000000;">rr</span><span style="color: #0000FF;">&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 292 ⟶ 620:
=={{header|PowerShell}}==
{{trans|J}} (Made more PowerShelly.)
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Switch-Gender ([string]$String)
{
Line 311 ⟶ 639:
Switch-Gender "She was a soul stripper. She took my heart!"
Switch-Gender (Switch-Gender "She was a soul stripper. She took my heart!")
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 319 ⟶ 647:
 
=={{header|Python}}==
<langsyntaxhighlight Pythonlang="python">#!/usr/bin/env python
# -*- coding: utf-8 -*- #
Line 442 ⟶ 770:
return "".join([ word+switch[gen] for word,gen in zip(text[::2],text[1::2])])+text[-1]
print rev_gender(text)</langsyntaxhighlight>
'''Output:'''
<pre>
Line 457 ⟶ 785:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang at-exp racket
 
Line 552 ⟶ 880:
by the cannibal propensity he nourished in his untutored youth.
}))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 567 ⟶ 895:
by the cannibal propensity she nourished in her untutored youth.
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Mechanically, this task is trivial. Raku provides several flexible and powerful methods to wrangle text. Linguistically, this task is impossible (and laughable). Mappings are non-regular and in many cases, non-deterministic without semantic analysis of the content and context, which is '''WAY''' beyond what anyone is going to invest in a Rosettacode task. Whatever.
 
For extremely limited circumstances such as this example, this should suffice. Notice case matching and replication. Handles contractions, but ignores embedded matching text.
 
<syntaxhighlight lang="raku" line>say S:g:ii/«'she'»/he/ given "She was a soul stripper. She took my heart! They say she's going to put me on a shelf.";</syntaxhighlight>
{{out}}
<pre>He was a soul stripper. He took my heart! They say he's going to put me on a shelf.</pre>
 
=={{header|REXX}}==
Line 575 ⟶ 913:
 
Given the large size of the table (list), it would make more sense to put the words in a separate file instead of coding them in-line (within the computer program).
<langsyntaxhighlight lang="rexx">/*REXX program reverse genderizes a text string (that may contain gender-specific words)*/
parse value linesize()-1 with sw @ @. !. /*get screen width, nullify some vars.*/
parse arg old
Line 590 ⟶ 928:
call tell old, ' old ' /*show a nicely parsed "old" text. */
 
@=@ "abboty abbess"
@=@ "actor actress"
@=@ "ad-boy ad-girl ad-man ad-woman ad-men ad-women"
Line 1,278 ⟶ 1,616:
if $\='' then say strip($)
say
return</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]]. <br>
Line 1,317 ⟶ 1,655:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Reverse the gender of a string
 
Line 1,385 ⟶ 1,723:
next
return alist2
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,403 ⟶ 1,741:
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/cpBaoMf/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/0dajvapgRRChRZaZgpeqnQ Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">object RevGender extends App {
val s = "She was a soul stripper. She took my heart!"
println(cheapTrick(s))
Line 1,414 ⟶ 1,752:
}
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var male2female = <<'EOD'
maleS femaleS, maleness femaleness, him her, himself herself, his her, his
hers, he she, Mr Mrs, Mister Missus, Ms Mr, Master Miss, MasterS MistressES,
Line 1,484 ⟶ 1,823:
func reverse_gender(text) {
text.gsub(gen_re, { |a| copy_case(a, dict{a.lc}) })
}</langsyntaxhighlight>
 
Example:
<langsyntaxhighlight lang="ruby">say reverse_gender("She was a soul stripper. She took my heart!");</langsyntaxhighlight>
{{out}}
<pre>He was a soul stripper. He took my heart!</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl"># Construct the mapping variables from the source mapping
apply {{} {
global genderMap genderRE
Line 1,545 ⟶ 1,884:
string map $genderMap &
]}]
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">puts [reverseGender "She was a soul stripper. She took my heart!"]\n
puts [reverseGender "When a new-hatched savage running wild about his native
woodlands in a grass clout, followed by the nibbling goats, as if
Line 1,556 ⟶ 1,895:
who were the wives of unconquerable warriors. There was excellent
blood in his veins-royal stuff; though sadly vitiated, I fear,
by the cannibal propensity he nourished in his untutored youth."]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,570 ⟶ 1,909:
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}}
<syntaxhighlight lang="wren">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))</syntaxhighlight>
 
{{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,476

edits