There have been a couple of previous attempts to ask this question, but neither conforms to modern standards on this site. Per discussion on Meta, I'm reposting it in a way that allows for fair competition under our modern rulesets.


A is a string that "reads the same forwards and backwards", i.e. the reverse of the string is the same as the string itself. We're not talking about "convenient palindromes" here, but a strict character-by-character reversal; for example, ()() is not a palindrome, but ())( is.

The task

Write a program or function that takes a string S (or the appropriate equivalent in your language) as input, and has one output Q (of a type of your choice). You can use any reasonable means to take the input and provide the output.

  • When the input S is a palindrome, the output Q should have a value A (that is the same for any palindromic S).
  • When the input S is not a palindrome, the output Q should have a value B (that is the same for any non-palindromic S).
  • A and B must be distinct from each other.

Or in other words: map all palindromes to one value, and all non-palindromes to another.

Additionally, the program or function you write must be a palindrome itself (i.e. its source code must be palindromic), making this a challenge.


  • Although true and false are obvious choices for A and B, you can use any two distinct values for your "is a palindrome" and "isn't a palindrome" outputs, which need not be booleans.
  • We're defining string reversal at the character level here; éé is palindromic regardless of whether the program is encoded in UTF-8 or Latin-1, even though it's not a palindromic sequence of octets after UTF-8 encoding.
  • However, even if your program contains non-ASCII characters, it only needs to work for ASCII input. Specifically, the input S will only contain printable ASCII characters (including space, but not including newline). Among other things, this means that if you treat the input as a sequence of bytes rather than a sequence of characters, your program will still likely comply with the specification (unless your language's I/O encoding is very weird). As such, the definition of a palindrome in the previous bullet only really matters when checking that the program has a correct form.
  • Hiding half the program in a comment or string literal, while being uncreative, is legal; you're being scored on length, not creativity, so feel free to use "boring" methods to ensure your program is a palindrome. Of course, because you're being scored on length, parts of your program that don't do anything are going to worsen your score, so being able to use both halves of your program is likely going to be helpful if you can manage it.
  • Because the victory criterion is measured in bytes, you'll need to specify the encoding in which your program is written to be able to score it (although in many cases it will be obvious which encoding you're using).

Victory criterion

Even though the program needs to be a palindrome at the character level, we're using bytes to see who wins. Specifically, the shorter your program is, measured in bytes, the better; this is a challenge. In order to allow submissions (especially submissions in the same language) to be compared, place a byte count for your program in the header of your submission (plus a character count, if it differs from the number of bytes).

  • 12
    \$\begingroup\$ Would someone please explain why would ()() not be a palindrome?? \$\endgroup\$ – Emilio M Bumachar Feb 20 '17 at 6:38
  • 58
    \$\begingroup\$ @EmilioMBumachar Try replacing ( with a and ) with b. Is abab a palindrome? No, it would have to be abba. Then ()() isn't a palindrome either; it would have to be ())(. \$\endgroup\$ – DLosc Feb 20 '17 at 6:40
  • 7
    \$\begingroup\$ Those solutions entirely using comments to make the program palindromic looks like a loophole to me :( \$\endgroup\$ – kennytm Feb 20 '17 at 8:26
  • 15
    \$\begingroup\$ @kennytm Disallowing them would be worse, because there's no satisfactory way to do that objectively in a language-agnostic way. (What's a comment? What about putting the unused half in a string literal that is discarded? What about 2D languages where you can have perfectly executable code that is simply never reached?) \$\endgroup\$ – Martin Ender Feb 20 '17 at 9:08
  • 9
    \$\begingroup\$ ()() is not a palindrome, but ())( is. Congratulations, you made it onto reddit! \$\endgroup\$ – numbermaniac Feb 25 '17 at 6:31

64 Answers 64


Jelly, 3 bytes


Try it online!

Added for the sake of completeness. This uses the fairly new quick Ƒ, which checks whether the argument is the same as the result of the atom being applied. Here, the atom is U, which reverses the argument. The quicklink returns either 1 for palindromes, or 0 otherwise. The second U has no effect on either integer, so leaves it as is.


Pascal (FPC), 139 bytes

uses strutils;var s:string;begin read(s);write(s=ReverseString(s))end.dne))s(gnirtSesreveR=s(etirw;)s(daer nigeb;gnirts:s rav;sliturts sesu

Try it online!

The program compares if the input is equal to its reverse using ReverseString() function from module strutils.

end. is the last part of the program. This program does not use comments because, in most cases, FPC does not care what is after end., so the program without the dot is simply reversed and appended to the program itself.


T-SQL, 70 bytes


Didn't see a SQL version, so here's my try. Returns 1 for a palindrome, and 0 otherwise. IIF is supported by SQL 2012 and later.

Input is via pre-existing table t with varchar field s, per our IO rules. The input table can include multiple rows.

The -- in the middle is a comment character.

Edit: Here's a more clever version, using a strangely-named input table instead of an extended comment. Unfortunately slightly longer (73 bytes):


This requires an input table with a long strange name:

] (s VARCHAR(999))

Note the return is included in the table name (not a recommended naming strategy for your databases, by the way).

Tables with otherwise invalid characters in their name need brackets or double-quotes around them, and the only way I could put the required quote at the beginning was to set it after a comment.


C# (.NET Core), 94


Improves slightly on the answer from Anderson Pimentel

Try it online


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy