# 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. Jun 17, 2016 at 17:14
• There should be bonus points if the program doesn't use s nor S anywhere in it. Jun 17, 2016 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? Jun 17, 2016 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 Jun 17, 2016 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!!! Jun 18, 2016 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. Jun 17, 2016 at 15:02
• @JoeZ. I just replaced the s by th, like everybody else did: look at those Pyson answers. Jun 17, 2016 at 16:04
• @coredump They're all wrong: it's one-way: s->th, S->Th, th->th, Th->Th. Jun 17, 2016 at 16:05
• @DrGreenEggsandIronMan Sure, (defmacro cathe (&rest args) (case ,@args)) Jun 17, 2016 at 18:07
• Ok lol that makes more sense
– user54200
Jul 5, 2016 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"? Jun 17, 2016 at 15:02
• I was thinking in 'Tt'[b>'r']+'h', but it is the same length Jun 17, 2016 at 18:48
• You mean 38 byteth Jun 17, 2016 at 19:41
• +1 for TIL that string comparison is done via Code Point Value
– bren
Jun 17, 2016 at 21:08
• @Upgoat That was my fault sorry.
– Neil
Jun 19, 2016 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, 2016 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! Jun 17, 2016 at 15:23 • Can I ask you, why you have 2 separate accounts? Jun 17, 2016 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. Jun 18, 2016 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! Jun 18, 2016 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 ! Jun 17, 2016 at 13:51 • s(c){c=getchar();c+=c-83&~32?0:26625;s(printf(&c));} Jun 17, 2016 at 13:58 • You can't push 2 bytes into a char. 'h\1' Jun 18, 2016 at 7:53 • @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Multi-character constants are implementation defined, bu perfectly valid C. Jul 5, 2016 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? Jul 17, 2016 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 Jun 17, 2016 at 12:23 • Oh, yeah you're right. Thanks! @aditsu Jun 17, 2016 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") Jun 17, 2016 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

• gsub(/s/,"th")gsub(/S/,"Th") does the trick. May 15 at 15:41

# 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. Jul 14, 2016 at 22:45
• Thanks for the note, fixed now. I don't know how I forgot to save Str1 back again... Jul 15, 2016 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. Jul 18, 2016 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"));}}


## 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! Jun 17, 2016 at 10:39
• lambda s:s.replace("s","th").replace("S","Th") is a bit shorter. Jun 17, 2016 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. Jun 17, 2016 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. Jun 17, 2016 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 Jun 17, 2016 at 11:12

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

• You don't need the space: f's'=... Jun 17, 2016 at 14:22
– Lynn
Jun 17, 2016 at 14:36
• Oh damn I completely forgot that. I almost never need Chars... Jun 17, 2016 at 14:51
• I thought it wath "Hathkell" Jun 17, 2016 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. Jun 17, 2016 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? Jun 17, 2016 at 21:04
• @mbomb007, who's to say someone won't create a different language called "Rust" in the future? Jun 17, 2016 at 21:24
• @msh210 Moot point, because if used the poster will need to clarify. Jun 17, 2016 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 Jun 17, 2016 at 16:03
• -8 bytes: remove quotes. -> use php -d error_reporting=0 to suppress notices. Jul 6, 2016 at 10:03

# 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 Aug 7, 2016 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.

# 35 Byteth of Julia:

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? ;) Jul 29, 2016 at 18:02

## 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 Jun 17, 2016 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) Jun 17, 2016 at 16:02
• @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Well, that's identical to TuukkaX's answer (which was posted before mine), so...
– m654
Jun 17, 2016 at 16:04
• There's no reason to post another answer then. Jun 17, 2016 at 16:08
• @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ I didn't notice his when I posted mine.
– m654
Jun 17, 2016 at 16:09
• You can delete your answer once you notice there is a shorter one posted before yours. Jun 17, 2016 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".

# s-lang, 1615 8 bytes (non-competing)

Saved one byte because s-lang no longer requires last argument bracket

Saved seven bytes because of ! paramater

### Try it online!

t![s][th


s-lang (or, th-lang) is a string manipulation language I am working on for fun. I am still optimizing argument brackets...

## Explantion

• t replace function with search argument and replace argument.
• ! "preserve case" parameter
• [s] the first search argument (searches for the character "s")
• [th the first replace argument (now this will replace each "s" with a "th"). End bracket is omitted because it is unnecessary.
• Doesn't p` parameter need +1 byte? Jul 4, 2016 at 18:36
• @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Oh.. it is now "!" instead of "!". I'll fix that Jul 9, 2016 at 22:58