1
\$\begingroup\$

This question already has an answer here:

Simple problem, create a programme that takes one input, and the outputs that input in reverse.

Some test cases are [input -> output]:

 Hello World! -> !dlroW olleH
 Code Golf -> floG edoC
 Reverse Cat Programme -> emmarporG taC esreveR

Shortest answer wins

Note: It doesn’t have to support multi-line inputs and it doesn’t matter how input is taken

\$\endgroup\$

marked as duplicate by Sriotchilism O'Zaic code-golf Jul 9 at 12:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    \$\begingroup\$ @O.O.Balance This one doesn't have unnecessary I/O restrictions \$\endgroup\$ – Adám Jul 9 at 10:34
  • 1
    \$\begingroup\$ @Adám Fair enough. \$\endgroup\$ – O.O.Balance Jul 9 at 10:36
  • 1
    \$\begingroup\$ Hi EdgyNerd, thanks for your contribution here. What happens if the input is an integer or a numeric value? There is nothing in your challenge that specifies that the input must be a string. \$\endgroup\$ – Shaun Bebbers Jul 9 at 10:57
  • 1
    \$\begingroup\$ Would be nice to specify whether multiline input strings have to be supported or not. \$\endgroup\$ – manatwork Jul 9 at 11:08
  • 2
    \$\begingroup\$ I'd suggest removing the (non-default) restriction to it being a program (standard jargon would be "a program or function" or just to remove the reference, e.g. "Simple problem output the input reversed".) \$\endgroup\$ – Jonathan Allan Jul 9 at 12:55

14 Answers 14

2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 30, 29 bytes

s=>string.Concat(s.Reverse())

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Keg, 4 1 byte

This is in the documentation:

^

The mechanism is simple: Reverse the whole stack. The stack is in fact a string, and input will trivially be the items in the stack.

There isn't an online implementation; download the Python interpreter and run this program, with the input prepending the ^.

\$\endgroup\$
2
\$\begingroup\$

Whitespace, 80 bytes

[S S S N
_Push_0][N
S S N
_Create_Label_LOOP][S N
S _Duplicate_top][S N
S _Duplicate_top][T N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve_input][S S S T    S T S N
_Push_10][T S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_PRINT_LOOP][S S S T N
_Push_1][T  S S S _Add][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT_LOOP][S S S T   N
_Push_1][T  S S T   _Subtract][S N
S _Duplicate_top][T T   T   _Retrieve_input][T  N
S S _Print_as_character][N
S N
S N
_Jump_to_Label_PRINT_LOOP]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Since Whitespace inputs one character at a time, the input should contain a trailing newline so it knows when to stop reading characters and the input is done.

Try it online (with raw spaces, tabs, and new-lines only).

Explanation in pseudo-code:

Integer i = 0

Start LOOP:
  Read STDIN as character, and store at heap-address i
  Character c = character at heap-address i
  If(c == '\n'):
    Jump to PRINT_LOOP
  i = i + 1
  Go to next iteration of LOOP

function PRINT_LOOP:
  i = i - 1
  Character c = character at heap-address i
  Print c as character to STDOUT
  Go to next iteration of PRINT_LOOP
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 1 byteSBCS

Anonymous tacit prefix function.

Try it online!

Do I need to explain this?

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 1 byte

R

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Japt, 1 byte

Ô

w also works.

Try it

\$\endgroup\$
1
\$\begingroup\$

ZX81 BASIC (Timex TS-1000/1500, ZX80 with 8K ROM etc... assuming sting input only) 87 tokenized BASIC bytes

 1 INPUT A$
 2 LET B$=""
 3 FOR I=LEN A$ TO 1 STEP -1
 4 LET B$=B$+A$(I)
 5 NEXT I
 6 PRINT A$;"->";B$

Very simple, enter a string (stored in A$) which is then transferred one character at a time to B$ from the last to first character in the FOR/NEXT loop between lines 3 through 5 inclusive.

Output is shown on line 6.

\$\endgroup\$
1
\$\begingroup\$

Haskell, 7 bytes

I think it is self explanatory.

reverse

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pyret, 65 bytes

{(x):string-from-code-points(string-to-code-points(x).reverse())}

Unfortunately Pyret doesn’t have a built in string reverse function, so you have to convert it to a list, reverse it, and convert it back.

\$\endgroup\$
1
\$\begingroup\$

Java 8, 14 bytes

s->s.reverse()

I/O is a StringBuilder.

Try it online.

35 bytes with String as I/O instead:

s->new StringBuffer(s).reverse()+""

Try it online.

\$\endgroup\$
1
\$\begingroup\$

Dart, 34 bytes

f(s)=>s.split('').reversed.join();

Try it online!

Pretty convoluted, you have to get a String List then reverse it and join it back for it to work.

\$\endgroup\$
1
\$\begingroup\$

Octave, 11 bytes

@(a)flip(a)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 1 byte

U

Try it online!

would also work here.

\$\endgroup\$
1
\$\begingroup\$

Python 3 (or 2), 16 bytes

lambda x:x[::-1]

Try it online!

Lambda that reverses its input

\$\endgroup\$

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