Perl, 45 bytes
;print$_=<>,~~reverse;m;esrever~~,><=_$tnirp;
Pretty straightforward, print
s the input ($_=<>
) followed by the reverse
of it. reverse
returns $_
because we're using it in scalar context by prefixing with ~~
. Then we match (m//
using ;
as delimiter), in void context, against the reverse of the script.
If we can guarrantee we won't have to create a palindrome of esrever,><=_$tnirp
we can shorten the code to 43 bytes:
g.print$_=<>,reverse.m.esrever,><=_$tnirp.g
Usage
echo -n 'neverod' | perl -e 'g.print$_=<>,reverse.m.esrever,><=_$tnirp.g'
neveroddoreven
Perl, 26 bytes
Includes 25 bytes code + 1 for -p
.
$_.=reverse;m;esrever=._$
I don't think this is valid since it requires the -p
flag which I don't think can be easily combined into the script contents to make a true palindrome. Pretty much the same calls as above, except it relies on the fact that -p
also adds a ;
behind the scenes (on newer Perls...) to close the m//
.
Usage
echo -n 'neverod' | perl -pe ';$_.=reverse;m;esrever=._$;'
neveroddoreven
2
This seems very similar to this question, except here we're generating instead of checking. Chances are the same tricks will be employed to make the program a palindrome, though.
– Sp3000 – 2015-03-16T09:02:52.1202I can totally understand the question downvote, but why was the answer downvoted? – John Dvorak – 2015-03-16T09:45:42.923
2
@JanDvorak I'm guessing its because it uses comments to make the palindrome, which specifically makes that strategy ok. It's not a very interesting way and is specifically banned in at least one question requiring palindromic code:http://codegolf.stackexchange.com/q/28190/15599 . Tomek, welcome to programming puzzles and codegolf. I'm upvoting anyway so you have access to our sandbox http://meta.codegolf.stackexchange.com/q/2140/15599 however I recommend you stick around and answer a few questions before you ask another one. Also, remember to search for similar questions before posting
– Level River St – 2015-03-16T10:48:46.800Are functions allowed (instead of whole programs)? – nimi – 2015-03-16T21:45:14.863
Can we use a delimiter for the palindrome generated? i.e.
neverod
->neverodadoreven
(with thea
in between) – Rɪᴋᴇʀ – 2016-04-25T23:38:12.557This question now has a duplicate: http://codegolf.stackexchange.com/questions/98325/palindromize-this-string
– Titus – 2016-11-03T14:15:20.907