# Lithp Tranthlator

My friend made a lisp translator the other day, that is to say it took a string and converted s=>th and S=>Th. It was quite long and I thought that it could be golfed.

So the task is to make a program/function that takes an input string, translates it in to lisp and outputs the string

Test case

Sam and Sally like Sheep        Tham and Thally like Thheep
Sally likes sausages            Thally liketh thauthageth
Sally sells seashells           Thally thellth theathhellth


Note that it doesn't matter that h gets repeated all the time

This is code golf so shortest answer wins

• I wished everyone would change the bytes in their headers to byteth. – Leaky Nun Jun 17 '16 at 17:14
• There should be bonus points if the program doesn't use s nor S anywhere in it. – Nate Diamond Jun 17 '16 at 18:41
• I think the language names should be made clear. Without the replacement. Some languages contain "th" already, so it's ambiguous. And who's to say that someone won't create a different language actually called "Common Lithp" in the future? – mbomb007 Jun 17 '16 at 21:02
• @mbomb007 I wouldn't worry about it too much. I'm not sure if there is actually a default way to title your answer. Most question I see if they do state how to do it it is normally the same way. As I haven't explain how the answer should be set out, then users are free to title them how they want to. Being a pedant, I didn't even ask for a language, so I could argue against them even writing them. But I do understand your point. I just don't think it is of concern – george Jun 17 '16 at 21:09
• A thing that would have made this challenge more interesting would be overall case preservation, e.g. LOOK OUT A SNAKE!!! -> LOOK OUT A THNAKE!!! – fluffy Jun 18 '16 at 3:06

## Common Lithp, 62

(map()(lambda(u)(princ(case u(#\s"th")(#\S"Th")(t u))))(read))


First, (read) the input (it should be a string). Strings in CL are sequences so we use map to iterate on each character. The first argument to map represents the type of the result (e.g. I could build a list from a vector). When it is nil, a.k.a. (), results are discarded. The function that is mapped to the input simply princ (print non-readably) each character, except the ones that should be replaced.

• That language name though. – Joe Z. Jun 17 '16 at 15:02
• @JoeZ. I just replaced the s by th, like everybody else did: look at those Pyson answers. – coredump Jun 17 '16 at 16:04
• @coredump They're all wrong: it's one-way: s->th, S->Th, th->th, Th->Th. – Erik the Outgolfer Jun 17 '16 at 16:05
• @DrGreenEggsandIronMan Sure, (defmacro cathe (&rest args) (case ,@args)) – coredump Jun 17 '16 at 18:07
• Ok lol that makes more sense – user54200 Jul 5 '16 at 15:20

# Retina, 9 bytes

S
Th
s
th


Try it online!

# JavaThcript ETh6, 38 bytes

At first I went with the obvious solution

a=>a.replace(/s/g,'th').replace(/S/g,'Th')


But I golfed it down 4 bytes

a=>a.replace(/s/gi,b=>b>'r'?'th':'Th')


This makes use of the regex i flag, which searches for case-insensitive patterns. The good thing about Javascript is that you can specify an anonymous function to handle (regex) replacing.

Try it here

f=
a=>a.replace(/s/gi,b=>b>'r'?'th':'Th')

s.innerHTML = [
'abScdsefSghsij',
'Sam and Sally like Sheep',
'Sally likes sausages',
'Sally sells seashells'
].map(c=>c + ' => ' + f(c)).join<br>
<pre id=s>

• "E eth thickth"? – Joe Z. Jun 17 '16 at 15:02
• I was thinking in 'Tt'[b>'r']+'h', but it is the same length – Washington Guedes Jun 17 '16 at 18:48
• You mean 38 byteth – Downgoat Jun 17 '16 at 19:41
• +1 for TIL that string comparison is done via Code Point Value – MayorMonty Jun 17 '16 at 21:08
• @Upgoat That was my fault sorry. – Neil Jun 19 '16 at 22:57

### GNU Sed - 17

s/S/Th/g;s/s/th/g


$sed -e "s/S/Th/g;s/s/th/g" Sam and Sally like Sheep Tham and Thally like Thheep Sally likes sausages Thally liketh thauthageth Sally sells seashells Thally thellth theathhellth  • Did you mean GNU Thed? ;) – m654 Jun 17 '16 at 15:57 # Jelly, 13 bytes “Ss”,“Th“th”y  Thhowing off Jelly'th thhiny new tranthlate atom. Try it online! # Python 3 - 40 byteth First golfing! lambda s:s.translate({115:'th',83:'Th'})  It uses the str module's translate method which accepts a translation table. The translation table is a simple dict with keycode as keys and the str in place of it as value. • Welcome to the PP&CG community! – Erik the Outgolfer Jun 17 '16 at 15:23 • Can I ask you, why you have 2 separate accounts? – Bálint Jun 17 '16 at 20:50 • @Bálint Apparently, he forgot to log into his current account with only 3 rep, but an experience on Stack Overflow. And posted with a new account. – user48538 Jun 18 '16 at 7:54 # JavaThcript ETh6, 43 byteth s=>s.replace(/s/gi,m=>({s:'th',S:'Th'})[m])  ## Tetht Thuite: th=s=>s.replace(/s/gi,m=>({s:'th',S:'Th'})[m]) console.log(th('Sam and Sally like Sheep')); console.log(th('Sally likes sausages')); console.log(th('Sally sells seashells')); • Confethth: thou only wroteth that tetht thuite for the thake of itth title! – ceased to turn counterclockwis Jun 18 '16 at 0:21 # V, 11 byteth Ís/th ÍS/Th  Try it online! V ith a language I made. It'th not finithed, but it'th really good at regekth. # C, 50 bytes s(c){c=getchar();c+=c-83&95?0:'h\1';s(printf(&c));}  Replace \1 with an actual \x01 byte. jimmy23013 saved a byte, and then I saved two more using his approach! Thanks. • I was about to comment that the &c parameter is broken. But it's not, because on a little-endian architecture the second byte of that int will be 0x00 and actually terminate the "string"... This is horribly clever, I love it ! – Quentin Jun 17 '16 at 13:51 • s(c){c=getchar();c+=c-83&~32?0:26625;s(printf(&c));} – jimmy23013 Jun 17 '16 at 13:58 • You can't push 2 bytes into a char. 'h\1' – Erik the Outgolfer Jun 18 '16 at 7:53 • @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Multi-character constants are implementation defined, bu perfectly valid C. – Dennis Jul 5 '16 at 2:23 ## 05AB1E, 14 12 bytes 's„th:'S„Th:  The straight forward answer. Try it online Saved 2 bytes thanks to @Adnan • Isn't this the thtraight forward anthwer rather than the straight forward answer? – dorukayhan Jul 17 '16 at 2:59 # Java, 71 65 bytes String t(String s){return s.replace("S","Th").replace("s","th");}  First attempt at golfing, so why not with Java. • You can use replace instead of replaceAll – aditsu quit because SE is EVIL Jun 17 '16 at 12:23 • Oh, yeah you're right. Thanks! @aditsu – Insane Jun 17 '16 at 12:25 • You can save some bytes by using a lambda expression instead of a function. s->s.replace("S","Th").replace("s","th") – Denker Jun 17 '16 at 12:45 # GNU AWK, 31 bytes Just using gsub function to translate lower or upper S via regex and print it afterwards. Can work with files, or with stdin as in this case $ awk '{gsub(/s/,"th");gsub(/S/,"Th")}1' <<< "This is Sparta"
Thith ith Thparta


## CJam, 15 bytes

q"sS""thTh"2/er


Test it here.

### Explanation

q      e# Read input.
"sS"   e# Push string.
"thTh" e# Push string.
2/     e# Split into pairs, i.e. ["th" "Th"].
er     e# Transliteration, replaces 's' with 'th' and 'S' with 'Th'.


## Python3 - 46 bytes

lambda s:s.replace("s","th").replace("S","Th")


Dropped 4 bytes with the help of @DenkerAffe!

• Python 3 was the language it was originally written in. My version of his code was 59 bytes, so we'll done! – george Jun 17 '16 at 10:39
• lambda s:s.replace("s","th").replace("S","Th") is a bit shorter. – Denker Jun 17 '16 at 10:51
• @DenkerAffe Yes it is shorter but to use lambda in this case, you would still need an input and an output to answer the original question. – george Jun 17 '16 at 10:55
• @george Community consensus is that functions can use arguments and returns values instead of using stdin/stdout. Have a look at our defaults for I/O. While you can of course override them if you want, it wouldn't make much sense in this particular challenge. – Denker Jun 17 '16 at 11:02
• @DenkerAffe okay that's fine. When I asked the question I mean the output would be an echo or print. But I shall leave it to the defaults. So yes using lambda would be shorter – george Jun 17 '16 at 11:12

f 's'="th";f 'S'="Th";f x=[x]
(>>=f)

• You don't need the space: f's'=... – ThreeFx Jun 17 '16 at 14:22
• Sadly, I do. Haskell names may contain apostrophes. :( – Lynn Jun 17 '16 at 14:36
• Oh damn I completely forgot that. I almost never need Chars... – ThreeFx Jun 17 '16 at 14:51
• I thought it wath "Hathkell" – Patrick Roberts Jun 17 '16 at 15:42

# Rust, 46 bytes

|s:&str|s.replace("s","th").replace("S","Th");


Try it online!

• @mbomb007. I'm not sure you should edit that. – Washington Guedes Jun 17 '16 at 21:02
• I'm making it follow the answering guidelines. I should be able to view a post and know what language it is. Some languages contain "th" already, so it's ambiguous. And who's to say that someone won't create a different language actually called "Rutht" in the future? – mbomb007 Jun 17 '16 at 21:04
• @mbomb007, who's to say someone won't create a different language called "Rust" in the future? – msh210 Jun 17 '16 at 21:24
• @msh210 Moot point, because if used the poster will need to clarify. – mbomb007 Jun 17 '16 at 21:32

# PHP, 42 Bytes

if run from a file:

<?=strtr($argv[1],["s"=>"th","S"=>"Th"]);  Run as: ~$ php [file] "This is Silly"

• -1 byte: remove the newline at the end – Erik the Outgolfer Jun 17 '16 at 16:03
• -8 bytes: remove quotes. -> use php -d error_reporting=0 to suppress notices. – Titus Jul 6 '16 at 10:03

# TI-Basic, 126 bytes

Input Str1
inString(Str1,"s
While Ans
sub(Str1,1,Ans-1)+"th"+sub(Str1,Ans+1,length(Str1)-Ans->Str1
inString(Str1,"s
End
inString(Str1,"S
While Ans
sub(Str1,1,Ans-1)+"Th"+sub(Str1,Ans+1,length(Str1)-Ans->Str1
inString(Str1,"S
End
Str1

• This is wrong. Str1 never changes, and Ans will hold a number at the end. – lirtosiast Jul 14 '16 at 22:45
• Thanks for the note, fixed now. I don't know how I forgot to save Str1 back again... – Timtech Jul 15 '16 at 23:08
• This is still wrong; it errors when the first or last character is S. As I've said before, please test your code before you post it. – lirtosiast Jul 18 '16 at 4:24

# Java, 101 byteth

interface a{static void main(String[]A){System.out.print(A[0].replace("S","Th").replace("s","th"));}}


Note that this is a complete program unlike the previous Java answer.

Bonus (has to be fed to the C preprocessor THEE preprothethor first):

#define interfaith interface
#define thtatic static
#define Thtring String
#define Thythtem System
#define replaith(x,y) replace(x,y)

interfaith a{thtatic void main(Thtring[]A){Thythtem.out.print(A[0].replaith("S","Th").replaith("s","th"));}}


## C# 6.0 - 58 bytes

string f(string s)=>s.Replace("s","th").Replace("S","Th");


Takes the input string as an argument to the function.

# Pyth, 14 bytes

::z\s"th"\S"Th


Test suite

Thanks to @LeakyNun for spotting a typo!

• So the language is "Pys"? :P – mbomb007 Jun 17 '16 at 21:00

# MATL, 17 bytes

115'th'YX83'Th'YX


Try it online!

### Explanation

115    % 's' (ASCII)
'th'   % String 'th'
YX     % Regex replacement
83     % 'S' (ASCII)
'Th'   % String 'Th'
YX     % Regex replacement


# Python 3, 53 bytes

def l(s):return s.replace("s","th").replace("S","Th")


Usage:

>> l('Sam and Sally like Sheep')

Tham and Thally like Thheep

• -7 bytes: lambda s:s.replace("s","th").replace("S","Th") Usage: (lambda s:s.replace("s","th").replace("S","Th"))(s) – Erik the Outgolfer Jun 17 '16 at 16:02
• @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Well, that's identical to TuukkaX's answer (which was posted before mine), so... – m654 Jun 17 '16 at 16:04
• There's no reason to post another answer then. – Erik the Outgolfer Jun 17 '16 at 16:08
• @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ I didn't notice his when I posted mine. – m654 Jun 17 '16 at 16:09
• You can delete your answer once you notice there is a shorter one posted before yours. – Erik the Outgolfer Jun 17 '16 at 16:11

# GameMaker Language, 74 bytes

return string_replace_all(string_replace_all(argument0,'s','th'),'S','Th')


## Matlab, 39 byteth

A straitforward approach:

@(t)strrep(strrep(t,'s','th'),'S','Th')


# Emacth Lithp, 61 byteth

(lambda(s)(replace-regexp-in-string"[Ss]\$$\\w*\$$""th\\1"s))


Emacs Lisp tries to be smart when replacing text, but that smartness breaks when the replaced string only takes up one space, i.e. the capital letter S. To prevent this from converting "Sam and Sally" to "THam and THally", the whole word is matched instead. However, this also handles "SAM and Sally" in the way that one would want, i.e. producing "THAM and Thally".

# x86 machine code, 19 bytes

In hex:

86ac3c5374043c73750440aab068aa84c075eec3


Input: ESI: input string, EDI: output buffer.

Disassembly:

_loop:
0:  ac          lodsb
1:  3c 53       cmp al,'S'
3:  74 04       je _th
5:  3c 73       cmp al,'s'
7:  75 04       jne _nth
_th:
9:  40          inc eax     ;[Ss]->[Tt]
a:  aa          stosb
b:  b0 68       mov al,'h'
_nth:
d:  aa          stosb
e:  84 c0       test al,al
10: 75 ee       jnz _loop
12: c3          ret

• You can use test al, 'S' to check both at once – anatolyg Aug 7 '16 at 22:40

# Befunge 98, 37 49 bytes

Original version :

~:"s"- #v_$"ht",>, _;#-"S":<;$"hT",^


Terminating edition, as per consensus :

~:a-!#@_:"s"-#v_$"ht",>, _;#-"S": <;$"hT",^


This leaves a big honking hole in the code grid that I'm not very happy about. I'll look into that when I have the time.
The 49th byte is a space at the end of the second line, included to have a rectangular grid, required to prevent ccbi (and probably other interpreters) from bugging out and printing an infinite line of "Th"s.

s->replace(s,r"s"i,s->"$(s[1]+1)h")  Try it online! (includes all test cases) • Hey, don't I know you from somewhere? ;) – Alex A. Jul 29 '16 at 18:02 # SpecBAS ThpecBATh - 53 bytes 1 INPUT a$: ?REPLACE$(REPLACE$(a\$,"S","Th"),"s","th")
`