14
\$\begingroup\$

When challenges require to read a string via STDIN/Console I have seen people interpreting that in mainly two ways. The first one was that you actually just have to type the string as

This is an example string.

the second way required you to also type some sort of delimiter (usually double " or single ' quotes)

"This is an example string."

or

'This is an example string.'

The question now is: Which one of those does actually count as string input via STDIN?

I am not sure, but I can imagine that there are languages where only one of those methods is possible for accepting strings as input.

On the other hand it might be easier (in codegolf: shorter) in some languages to use one or the other way.

\$\endgroup\$
25
\$\begingroup\$

Unless the challenge specifies, either is acceptable

Just be sure to mention what form of input your program requires in your explanation.

\$\endgroup\$
  • 3
    \$\begingroup\$ I don't think it really matters; all it really comes down to is how your language accepts input. If they disallow certain types of input, then yeah, of course you can't use it, but otherwise, why not? \$\endgroup\$ – Addison Crump Dec 1 '15 at 12:05
  • \$\begingroup\$ Is it correct that you mean as long as the OP does not explicitly say that the input cannot be assumed to be delimited, you can assume the string to be or not to be delimited if you want to submit a program that requires it to be either way? \$\endgroup\$ – flawr Dec 1 '15 at 13:08
  • \$\begingroup\$ @flawr this is exactly what I mean \$\endgroup\$ – quintopia Dec 1 '15 at 17:07
6
\$\begingroup\$

Without quotes

I have always assumed that quotes weren't allowed.

In the ideal case, I think standard input should be the same for everyone and contain the challenge input as plainly as possible. This is how online judges work (e.g. codegolf.com), although I realise PPCG is a bit different.

Allowing stdin to be pre-formatted with quotes seems unnecessary, and will inevitably be abused. I'm not aware of any languages that read from stdin but strictly require quotes.

It's also worth pointing out that up to 541 existing Python 2 answers could be shortened by using input() with quotes around each line in place of raw_input().

\$\endgroup\$
  • \$\begingroup\$ Abusing quotes in Haskell: you can use readLn instead of getLine if the input is surrounded with ". \$\endgroup\$ – nimi Dec 1 '15 at 16:24
  • \$\begingroup\$ Also, Mathematica could use Input[] instead of InputString[]. \$\endgroup\$ – LegionMammal978 Dec 1 '15 at 22:04
  • \$\begingroup\$ I prefer this method because it avoids potential problems with the " characters needing to be in the input. I also think it can help with people trying to coerce the challenge into just being an eval(input()) sort of deal. \$\endgroup\$ – FryAmTheEggman Dec 3 '15 at 1:16
  • \$\begingroup\$ Python 2: input() accepts "double-quoted strings" and 'single-quoted strings' as input, as opposed to raw_input(), which accepts non-quoted strings for +4 bytes. That's an example of why your answer is not good. \$\endgroup\$ – Erik the Outgolfer Jul 4 '16 at 22:18
  • 1
    \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ what? \$\endgroup\$ – grc Jul 5 '16 at 2:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .