Given a character as input, determine if your source code contains that character or not.
Shortest code wins. Though, I'd like to see some "normal" languages too.
UPDATE: the question previosly contained a restriction about using a function whenever possible, now I removed it.
-
1\$\begingroup\$ Related. \$\endgroup\$Neil– Neil2025年06月28日 14:30:51 +00:00Commented Jun 28 at 14:30
-
1\$\begingroup\$ Related: Input ∩ Source Code, Counting characters, \$\endgroup\$xnor– xnor2025年06月28日 18:05:28 +00:00Commented Jun 28 at 18:05
-
3\$\begingroup\$ For many languages (not just Zsh as in @pxeger's answer) an empty program produces empty output, which is falsy whereas non-empty output is truthy; and falsy / truthy is an acceptable output criterion in decision problems. So the empty program will (uninterestingly) fulfill the challenge requirements \$\endgroup\$Luis Mendo– Luis Mendo2025年06月28日 20:18:35 +00:00Commented Jun 28 at 20:18
-
4\$\begingroup\$ I note that this question is tagged [quine] but does not seem to invoke the rules for quine-related challenges, which prohibit reading the source. Perhaps it should be tagged [self-referential] instead. \$\endgroup\$Steve Bennett– Steve Bennett2025年06月29日 12:42:24 +00:00Commented Jun 29 at 12:42
-
\$\begingroup\$ Very related / possibly a duplicate (only difference is that this challenge asks for a character as input, and that challenge for a substring 🤷). EDIT: Based on the difference in answers, it seems to be different enough to not warrant a duplicate imo. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2025年06月30日 09:21:54 +00:00Commented Jun 30 at 9:21
42 Answers 42
brainfuck, 86 bytes
>,>+++++[<------>-]<<<++++[->>------------<++++[->->+<[>-]>[->>+<]<<<]<]>>>>>.;=KLMN\^
APL+WIN, (削除) 12 (削除ここまで) 8 bytes.
-4 bytes thanks to Adam
Prompts for input character. 1 if it exists in src code 0 if not.
⎕∊'∊⎕'''
-
\$\begingroup\$ I get 0 running on input ⎕. \$\endgroup\$Tbw– Tbw2025年06月28日 15:58:06 +00:00Commented Jun 28 at 15:58
-
\$\begingroup\$ @Tbw Thanks. Fixed. \$\endgroup\$Graham– Graham2025年06月28日 16:06:15 +00:00Commented Jun 28 at 16:06
-
\$\begingroup\$ Still doesn't account for the single quote. Probably just needs two more bytes. \$\endgroup\$Tbw– Tbw2025年06月28日 17:33:38 +00:00Commented Jun 28 at 17:33
-
\$\begingroup\$ @Tbw. Thanks again. Hopefully this time ;) \$\endgroup\$Graham– Graham2025年06月28日 19:10:57 +00:00Commented Jun 28 at 19:10
-
\$\begingroup\$ Why
+/like that and not just⎕∊'⎕∊'''? or even⍞∊'⍞∊'''. \$\endgroup\$Adám– Adám2025年07月24日 06:07:00 +00:00Commented Jul 24 at 6:07
C64 Basic, 100 bytes
The output indicates the position of the searched character within the source code, otherwise it is 0.
Pip, 11 bytes
{}(); aNSTf
Outputs 0 for "does not contain" or a positive number for "does contain." Attempt This Online!
Explanation
{}(); aNSTf
f Main function
ST Stringify
aN Count occurrences of command-line argument in that string
{}(); The stringification adds these characters, so they need to be added
to the code as some no-ops
Questionable but interesting 10-byte solution
aQ_MRXWSTf
Outputs 0000000000 for "does not contain" or a string of nine 0s and one 1 for "does contain." Technically, if these were treated as strings in Pip, all-zeroes would be falsey and not-all-zeroes would be truthy. However, behind the scenes, they're actually lists, which would all be considered truthy, so I don't think this output method quite passes muster. It could be fixed by adding _MS at the beginning, but that's longer than my main solution.
Explanation
aQ_MRXWSTf
STf Stringify main function
MR Find every regex match of
XW Word character (letter/number/underscore)
MR and map this function to it:
aQ_ Is it equal to the command-line argument?
In other words, filter out all the extra characters added during stringification before checking if the program argument is in the result.
Bespoke, 85 bytes
AAAAA@AA@AAAA@AA@AAAAAAAA@AAAAAAAAAA@AAA@AA@AAA@AA@AAAAAAAA@AAAAA@AAAAAAAA@A@AAAAAA@A
Equivalent to the following code (in "mnemonic" form):
INPUT CH
PUSH BI STACKTOP QUOTIENTOF
PUT XX:TRI BI;STACKTOP MINUS
STACKTOP F
OUTPUT N
Alice, 17 bytes
' wt.0g!].$KI(?O@
Takes the searched character as input. Prints to stdout the character if found or nothing otherwise.
' wt.0g!].$KI(?O@
' # Pushes 32, the ascii code of the space char
wt .$K # For each `i` between 32 to 0
.0g # Pushes the character at position (`i`, 0) in the source code as its ascii code
!] # Pops, writes it to the tape an shift the tape right
I( # Reads an argument from the input and search it on the tape
? # Read the tape at the current position and pushes the value
O@ # Print to stdout if found then exit
💎
Created with the help of Luminespire.
Alice, 28 bytes without source reading
'"''"w!]d$KI(?O@"w!]d$KI(?O@
sed -n, 15 bytes
/[]\[\\\/q1]/q1
Reads from standard input, exit code 1 if contained in the source, 0 otherwise.
-
4\$\begingroup\$ Welcome to Code Golf! unfortunately, our rules for the
quinetag prohibit reading your own source code, as that's considered an improper quine. \$\endgroup\$Themoonisacheese– Themoonisacheese2025年06月30日 09:29:17 +00:00Commented Jun 30 at 9:29 -
\$\begingroup\$ 42 bytes legal version \$\endgroup\$Themoonisacheese– Themoonisacheese2025年06月30日 09:32:01 +00:00Commented Jun 30 at 9:32
Explore related questions
See similar questions with these tags.