# “Multiply” two strings

This was inspired by a function I recently added to my language Add++. Therefore I will submit an short answer in Add++ but I won't accept it if it wins (it wouldn't be fair)

Don't you hate it when you can multiply numbers but not strings? So you should correct that, right?

You are to write a function or full program that takes two non-empty strings as input and output their multiplied version.

How do you multiply strings? I'll tell you!

To multiply two strings, you take two strings and compare each character. The character with the highest code point is then added to the output. If they are equal, simply add the character to the output.

Strings are not guaranteed to be equal in length. If the lengths are different, the length of the final string is the length of the shortest string. The input will always be lowercase and may contain any character in the printable ASCII range (0x20 - 0x7E), excluding uppercase letters.

You may output in any reasonable format, such as string, list etc. Be sensible, integers aren't a sensible way to output in this challenge.

With inputs of hello, and world!, this is how it works

hello,
world!

w > h so "w" is added ("w")
o > e so "o" is added ("wo")
r > l so "r" is added ("wor")
l = l so "l" is added ("worl")
d < o so "o" is added ("worlo")
! < , so "," is added ("worlo,")

So the final output for hello, and world! would be worlo,!

# More test cases

(without steps)

input1
input2 => output

programming puzzles & code golf!?
not yet graduated, needs a rehaul => prtgyetmirgduuzzlesneedsde rolful

king
object => oing

blended
bold => boln

lab0ur win.
the "super bowl" => the0usuwir.

donald j.
trumfefe! => trumlefj.

This is a so shortest code wins! Luok!

• This is the elementwise maximum of the strings, right? That doesn't seem anything like multiplying. – xnor Jun 8 '17 at 22:34
• Nitpick: PPCG has graduated, we just didn't get a new design yet. – Dennis Jun 9 '17 at 4:02
• Possible relevant: codegolf.stackexchange.com/questions/74809/merging-two-strings/… – nmjcman101 Jun 9 '17 at 11:40

# Micro, 119 bytes

{R b i m+:R}:X{R a i m+:R}:Y
""\\:a:b:R
{b:M}:N
a:M
b a<if(N,)
0:i{i1+:i
a i m C b i m C~:> if(X,Y)
i M=if(,Z)}:Z
Z
R:\

# C#, 71, 67 bytes

saved a few bytes by using string.Join() instead of new string()

added 18 bytes to both versions for needing using System.Linq;

67 bytes

(a,b)=>string.Join("",(a.Zip(b,(x,y)=>x>y?x:y)));

71 bytes:

(a,b)=>new string(a.Zip(b,(x,y)=>x>y?x:y).ToArray());

DotNetFiddle

# JavaScript (ES6), 47 bytes

f=([S,...s],[T,...t])=>S&&T?(S>T?S:T)+f(s,t):''

A recursive solution, which walks the string, always outputting the largest character.

Snippet:

f=([S,...s],[T,...t])=>S&&T?(S>T?S:T)+f(s,t):''

console.log(f('programming puzzles & code golf!?','not yet graduated, needs a rehaul'));
console.log(f('king','object'));
console.log(f('blended','bold'));
console.log(f('lab0ur win.','the "super bowl"'));
console.log(f('donald j.','trumfefe!'));

# Python 3, 29 bytes

lambda*a:''.join(map(max,*a))

Try it online!

# Julia 0.5, 33 bytes

Pretty much the same concept as Python2, but shorter.

a->join(map(maximum,(zip(a...))))

Try it online!

# k, 14 bytes

{|/(&/#:'x)$'x} Examples: k)F:{|/(&/#:'x)$'x}
k)F("hello,";"world!")
"worlo,"

q translation:

{max(min count each x)$/:x} Free interpreter available here • Can you provide a Try It Online or similar? At the moment, K doesn't produce the correct output on Kona or oK – caird coinheringaahing Jul 14 '17 at 19:36 • I added a link to download the interpreter. Kona is an implementation of an old version of k. oK is an implementation of the k6 spec (kparc.com/k.txt), which doesn't have a reference implementation from kx yet. – skeevey Jul 14 '17 at 19:46 # J, 22 bytes 0({"1\:~@,"0/)<.&#$&>;

Try it online!

## Explanation

0({"1\:~@,"0/)<.&#$&>; Input: string x (LHS), string y (RHS) # Get length of x and y <.& Minimum of those ; Link x and y as a pair of boxed strings$&>   Shape each to the minimum length
,"0/           Reduce by joining elementwise
0 {"1                   Select the value at index 0 from each
• unfortunately my idea f=:{.@/:~"1@|:@,: has trailing spaces and fails when one or other string is one character long – jayprich May 25 '18 at 2:34

# Acc!!, 186 bytes

N
Count i while _/128^i/32 {
_+128^(i+1)*N
}
_*128+N
Count i while _%128/32*(_/4096) {
Count j while _/128%128/(_%128+1) {
_/128^2*128^2+_/128%128+_%128*128
}
Write _%128
_/128^2*128+N
}

Input strings should be given on stdin, newline-separated. Try it online!

### Explanation

We conceptually partition the accumulator into 7-bit registers, each of which can hold an input character. The first Count i loops until the input's ASCII code is smaller than 32, storing the first line of input into the accumulator, with the first character in the least-significant position:

, o l l e h
5 4 3 2 1 0
Accumulator value = asc(h) + 128*asc(e) + 128^2*asc(l) + ... = 1541853098728

Next, we shift everything left by one register, read the first character of the second line, and store it into register 0:

, o l l e h w
6 5 4 3 2 1 0

We Count i again while register 0 (_%128) and register 1 (_/128%128, though the formula can be golfed a bit in this context) are both >= 32. Inside the loop, we use a nested Count j loop to check if register 1 is strictly greater than register 0. (Comparisons are done via integer division: if a/(b+1) is 0, then a<=b; if a/(b+1) is nonzero, then a>b.) If it is greater, we swap the registers' contents. This loop executes at most once, because after swapping, the condition is guaranteed not to be true anymore.

After the (potential) swap, the larger of the two characters is in register 0, which we output. We then shift two registers to the right (_/128^2), shift one register back to the left (*128), and store the next input character in register 0 (+N):

, o l l e o
5 4 3 2 1 0

The Count i loop exits once one or both strings reach the newline at their end.

# Java, 158 151 bytes

Golfed:

String s(String a,String b){String r="";for(int i=0;i<Math.min(a.length(),b.length());i++){char c=a.charAt(i);char d=b.charAt(i);r+=c>d?c:d;}return r;}

Ungolfed:

String s(String a,String b) {
String r = "";
for(int i=0; i < Math.min(a.length(),b.length()); i++) {
char c = a.charAt(i);
char d = b.charAt(i);
r += c > d ? c : d; //absolute ternary abuse
}
return r;
}

Takes two strings in a and b.

• I think you could save a few bytes by getting rid of the outside ternary if statement (r+=c==d?c:) since if they are equal you can add either c or d to the string and it won't make a difference. – 0 ' Dec 8 '17 at 2:18

# Prolog (SWI), 55 bytes

[A|T]*[B|U]-[C|R]:-(A>B,C=A;C=B),T*U-R.
[]*_-[].
_*L-L.

Try it online!

# Aceto, 43 bytes

]w(x^
o|p
d(
do
X0l)x
=X(
€(=0p
rr€l<

### Explanation:

First, we read the first string, and explode it, then we move a stack to the left, read the second string, and explode it:

€(
rr€

Now, we check for emptiness of either of the stacks and exit if that is the case:

X0l)
=X
=0
l

Next, we duplicate the top letter of both stacks, and convert them to code points, then move them both (and us) on the right stack:

]
o
d(
do

The w tests if something is less or equal. If so, we get mirrored to the right. Otherwise, we print the top letter, go on the left stack, drop the top element (the other letter) and move up (^), which moves us on the bottom <, which moves us back to the length check.

w(x^
|p

If however, the test was truthy, we get mirrored into the emptiness on the right, and we need to do the opposite. Eventually we reach the three commands that now drop, then move to the left stack and print its top element. The < is the same symbol we land on in the other case, so here too we are lead back to the length check.

x
(
p
<

This repeats until one of the length checks succeeds, i.e. if one of the input strings is exhausted.

# Modern Pascal

version 1 of algorithm (func:139bytes)

for var l:=1 to min(length(paramstr(1)),length(paramstr(2))) do
result+=iif(paramstr(1)[l]>paramstr(b)[l],paramstr(1)[l],paramstr(2)[l]);

Explanation The for loop is designed to crawl to the length of the shortest string. The output is compared appending to result the highest ASCII value of each letter crawled. Since we are using shortest length, we can access the strings elements directly - see v2 below, uses Copy() to be safer.

version 2 of algorithm (func:166bytes)

for var l:=1 to max(length(paramstr(1)),length(paramstr(2))) do
result+=iif(copy(paramstr(1),l,1)>copy(paramstr(2),l,1),copy(paramstr(1),l,1),copy(paramstr(2),l,1));

// Author of Modern Pascal

• Welcome to our site! This contest is a code-golf contest. You should aim to minimize the number of bytes in the source code. I don't know pascal at all so this may be well golfed already but I suspect there is some whitespace that can be removed. Once you are satisfied with your golfs you can add the byte count of your program to your header. If you have any questions you can ping me with @wheatwizard. – Post Rock Garf Hunter Jun 12 '17 at 13:49
• As Wheat Wizard said, I'm sure this can be golfed to reduce your score but I think that there is at least one typo in your first code, maybe two. Should iif(a[l]>b[l]),a[l] be if(a[l]>b[l],a[l]? (there is definitely an extra ) but I don't know about iif and if) – caird coinheringaahing Jun 12 '17 at 14:13
• shortened. Good find on the extra paren. fixed. In MP, I added iff() to mimic ?(eval,true,false). – Ozz Nixon Jun 13 '17 at 17:39

# Charcoal, 17 bytes

Ｆ⌊⟦ＬθＬη⟧⌈⟦§θι§ηι⟧

Try it online! Link is to verbose version of code. Edit: Slice got added a few days later, which would have allowed the following code for 15 bytes: Try it online!

↑Ｅ✂θ⁰Ｌη¹⌈⟦ι§ηκ⟧
• 9 bytes instead of 15, not sure if I had this back in June so maybe it's valid lol :P tio.run/##S85ILErOT8z5/… – ASCII-only Aug 28 '17 at 15:41

# Pip, 9 bytes

DQSS_MaZb

Try it online!

Pip doesn't have a string max operator, so I used SS (Sort String) instead:

aZb  Zip the two cmdline args together, truncating to shorter length
M     To that list of 2-element lists of characters, map a function:
SS_       Sort each pair of characters stringwise
DQ          and dequeue the second (larger) one
Autoprint the resulting list of characters

Try it online!

# R, 96 bytes

for(i in 1:min(lengths(m<-sapply(scan(,""),utf8ToInt))))cat(intToUtf8(max(m[[1]][i],m[[2]][i])))

Try it online!

Using ASCII code.