35
\$\begingroup\$

Your task is simple, write a single code snippet that when executed in one language outputs only the string 'abc' and when executed in another language outputs only the string 'cba'. The program should take no input.

This is a challenge.

\$\endgroup\$
9
  • 5
    \$\begingroup\$ I don't think this is a dupe at all. The fact that the string is reversed is different enough from printing two different strings. I won't (vote to) reopen, though, as that would have immediate effect \$\endgroup\$
    – Luis Mendo
    Commented Aug 9, 2017 at 20:00
  • 4
    \$\begingroup\$ I voted to reopen this post, because IMO printing the reverse of a String and a String is much different than 2 different ones. No answer can trivially be modified to fit this challenge. My own answer uses a reverse symmetry techinque when compared to the answers there. I agree with @LuisMendo. \$\endgroup\$
    – Mr. Xcoder
    Commented Aug 9, 2017 at 20:04
  • 2
    \$\begingroup\$ Can you print ABC instead of abc \$\endgroup\$
    – Oliver Ni
    Commented Aug 10, 2017 at 0:53
  • 5
    \$\begingroup\$ I vote to reopen, some answers use the fact cba is abc backwards; link, link, link, link, link, link, link, link, link \$\endgroup\$
    – Oliver Ni
    Commented Aug 10, 2017 at 2:37
  • 2
    \$\begingroup\$ 'ABC' and 'CBA' is ok, leading/trailing whitespace/newlines ok but must be the same for both outputs \$\endgroup\$ Commented Aug 10, 2017 at 7:56

64 Answers 64

1 2
3
0
\$\begingroup\$

PHP & HTML, 15 bytes

<?die(abc)?>cba

PHP exits with printing abc; cba is ignored.
HTML sees the PHP code as unrecognized tag (and ignores it), then prints cba.

\$\endgroup\$
0
\$\begingroup\$

Javascript and Japt, 18 15 bytes

$Pw=P="cba";$Pw

Outputs "abc" in Japt and "cba" Javascript.

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ This doesn't work in JS. \$\endgroup\$
    – Shaggy
    Commented Jul 8, 2021 at 19:53
  • \$\begingroup\$ @Shaggy Works fine when run the same way as Oliver's answer, but I see where you're coming from. This is an old post and approaches to standard ways to answering were different back then. \$\endgroup\$
    – Etheryte
    Commented Jul 8, 2021 at 20:19
0
\$\begingroup\$

Bash / 05AB1E, 27 bytes

"printf" "abc\n" '#' r.ÁR¦¦

Try it online Bash

"printf" "abc\n" '#' r.ÁR¦¦
 │        │       └useless arguments
 │        └abc\n argument
 └printf command

Try it online! 05AB1E

"printf" "abc\n" '#' r.ÁR¦¦
 │        │       │ ││ │  └remove first two chars
 │        │       │ ││ └rotate stack 1 right
 │        │       │ │└reverse stack
 │        │       │ └push space character to stack
 │        │       └push # character to stack
 │        └push string "abc\n" to stack
 └push string "printf" to stack

Bash / 05AB1E, 24 bytes (without new line)

"printf" "abc" '#' r.ÁR?

Try it online Bash

"printf" "abc" '#' r.ÁR?
 │        │     └useless arguments
 │        └abc argument
 └printf command

Try it online!

"printf" "abc" '#' r.ÁR?
 │        │     │ ││ │ └print stack without newline
 │        │     │ ││ └rotate stack 1 right
 │        │     │ │└reverse stack
 │        │     │ └push space character to stack
 │        │     └push # character to stack
 │        └push string "abc\n" to stack
 └push string "printf" to stack
\$\endgroup\$
1
  • \$\begingroup\$ "printf" "abc" r.ÁR? seems to work for 20. But I don't know either Bash or 05AB1E. \$\endgroup\$
    – Shaggy
    Commented Jul 8, 2021 at 10:32
0
\$\begingroup\$

A0A0 and FiM++, 123 bytes

(
>P97
P98
P99

)
Dear Princess Celestia: A!
Today I learned B.
I said "cba"!
That's all about B!
Your faithful student, C.

The parentheses at the top open a block comment in FiM++, so the A0A0 code in it is ignored by FiM++. A0A0 starts at the second line because of the preceding > and then print "abc", halting on the empty line after it. FiM++ ignores the block comment and just executes a basic program that prints "cba". The first and last lines are for the class declaration, the second and fourth lines are for the main method, with the third line actually printing "cba".

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.