Reverse the gender of a string: Difference between revisions
Content added Content deleted
(Changed Go entry so it is more in line with task requirements and moved it into correct alphabetical order.) |
|||
Line 228: | Line 228: | ||
{{out}} |
{{out}} |
||
<pre>He was a soul stripper. He took my heart! They say he's going to put me on a shelf.</pre> |
<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... |
|||
<lang Phix>constant words = {"she","he","his","her","him","her","hers","his"} |
|||
function reverse_gender(string s) |
|||
integer wordend, ch |
|||
bool inword = false, wordch |
|||
for i=length(s)-1 to 0 by -1 do |
|||
ch = iff(i=0?' ':s[i]) |
|||
wordch = not find(ch," .,'!\n") |
|||
if inword then |
|||
if not wordch then |
|||
string this = lower(s[i+1..wordend]) |
|||
integer k = find(this,words) |
|||
if k then |
|||
string rep = words[iff(mod(k,2)?k+1:k-1)] |
|||
if s[i+1]!=words[k][1] then rep[1] = upper(rep[1]) end if |
|||
s[i+1..wordend] = rep |
|||
end if |
|||
inword = false |
|||
end if |
|||
else |
|||
if wordch then |
|||
inword = true |
|||
wordend = i |
|||
end if |
|||
end if |
|||
end for |
|||
return s |
|||
end function |
|||
constant tests = {"She was a soul stripper. She took my heart!\n", |
|||
"Her dog belongs to him but his dog is hers!\n"} -- ha ha! |
|||
for i=1 to length(tests) do |
|||
string ti = tests[i], |
|||
r = reverse_gender(ti), |
|||
rr = reverse_gender(r) |
|||
puts(1,ti&r&rr&"\n") |
|||
end for</lang> |
|||
{{out}} |
|||
<pre> |
|||
She was a soul stripper. She took my heart! |
|||
He was a soul stripper. He took my heart! |
|||
She was a soul stripper. She took my heart! |
|||
Her dog belongs to him but his dog is hers! |
|||
His dog belongs to her but her dog is his! |
|||
Her dog belongs to his but his dog is her! |
|||
</pre> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |