Reverse the gender of a string: Difference between revisions

Content added Content deleted
(Added solution for Action!)
Line 37: Line 37:
He wants what's his, she wants his and he wants her!
He wants what's his, she wants his and he wants her!
His dog belongs to her but her dog is his!
His dog belongs to her but her dog is his!
</pre>

=={{header|Action!}}==
<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</lang>
{{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>
</pre>