Reverse the gender of a string: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: (preamble))
m (comments)
Line 240: Line 240:


=={{header|Phix}}==
=={{header|Phix}}==
Oh well, I tried...
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>
replacing the words right->left means no need to fiddle with indexes when lengths differ.
<lang Phix>constant words = {"she","he","his","her","him","her","hers","his"}
<lang Phix>constant words = {"she","he","his","her","him","her","hers","his"}


function reverse_gender(string s)
function reverse_gender(string s)
integer wordend, ch
integer ch, wordend
bool inword = false, wordch
bool inword = false, wordch
for i=length(s) to 0 by -1 do
for i=length(s) to 0 by -1 do
Line 253: Line 255:
string this = lower(s[i+1..wordend])
string this = lower(s[i+1..wordend])
integer k = find(this,words)
integer k = find(this,words)
if k then
if k then
string rep = words[iff(mod(k,2)?k+1:k-1)]
string rep = words[iff(mod(k,2)?k+1:k-1)]
-- if s[i+2..wordend]=rep[2..$] then -- might be wanted here
-- -- (either skipping completely or all upper->all upper)
if s[i+1]!=words[k][1] then rep[1] = upper(rep[1]) end if
if s[i+1]!=words[k][1] then rep[1] = upper(rep[1]) end if
s[i+1..wordend] = rep
s[i+1..wordend] = rep
Line 260: Line 264:
inword = false
inword = false
end if
end if
else
elsif wordch then
if wordch then
inword = true
inword = true
wordend = i
wordend = i
end if
end if
end if
end for
end for