Reverse the gender of a string: Difference between revisions

m
comments
m (→‎{{header|Haskell}}: (preamble))
m (comments)
Line 240:
 
=={{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>
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"}
 
function reverse_gender(string s)
integer wordendch, chwordend
bool inword = false, wordch
for i=length(s) to 0 by -1 do
Line 253 ⟶ 255:
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+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
s[i+1..wordend] = rep
Line 260 ⟶ 264:
inword = false
end if
elseelsif wordch then
ifinword wordch= thentrue
inwordwordend = truei
wordend = i
end if
end if
end for
7,805

edits