13
\$\begingroup\$

Given a string s and a non-negative number n less than the length of s, output the character at the n-th position of s.

0-indexing and 1-indexing are allowed. For 1-indexing, n will be positive and less than or equal to the length of s.

s will consist of printable characters only.

Any reasonable input/output is permitted. Standard loopholes apply.

Testcases (0-indexing):

n s        output
0 "abcde"  a
1 "abcde"  b
2 "a != b" !
3 "+-*/"   /
4 "314159" 5

Testcases (1-indexing):

n s        output
1 "abcde"  a
2 "abcde"  b
3 "a != b" !
4 "+-*/"   /
5 "314159" 5

This is , so shortest answer in bytes wins.

\$\endgroup\$
  • 13
    \$\begingroup\$ I downvoted this because it's​ not really a programming or golfing challenge; all that's essentially being asked here is which language has the shortest built-in​ for the job. \$\endgroup\$ – Shaggy May 13 '17 at 14:11
  • 15
    \$\begingroup\$ @Shaggy Same for many other simple challenges like adding two numbers, testing if a number is prime or printing "Hello, World!". While these are boring in many languages that can do them out of the box, they can be interesting challenges for more primitive languages that have to roll their own implementation. Plus, anything more complicated is usually too much to ask of those languages, so it's nice to have an outlet for them. If trivial challenges bore you, try doing them in a nontrivial language. \$\endgroup\$ – Martin Ender May 13 '17 at 14:15

65 Answers 65

0
\$\begingroup\$

Julia 0.6.0 (13 bytes)

f(s,n)=(s[n])

s is a string, n the index. Pretty simple since Julia considers strings as arrays so I just call the index in the array. Julia is a 1 indexing.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Cubically, 18 16 bytes

$:7~(~*1-1/1)6@7

(1-based index)

Input format is n abcde, where n is the index, and abcde is the string.

How it works:

$:7              Get numeric input and set notepad to it
   ~             Skip next character (the space)
    (       )6   Do while the notepad is nonzero
     ~           Get next character of input
      *1-1/1     Subtract 1 from notepad (found by Kamil Drakari)
              @7 Output the current input character

Try it online

\$\endgroup\$
0
\$\begingroup\$

T-SQL, 29 bytes

SELECT SUBSTRING(s,n,1)FROM t

Input is taken via a pre-existing table t with integer n and string s, per our IO standards.

\$\endgroup\$
0
\$\begingroup\$

Rebol, Red 4 bytes

s/:n

(need to make body at least 30 to satisfy submission nanny)

\$\endgroup\$
  • \$\begingroup\$ Hello and welcome to PPCG. You can use the HTML comment tag <!-- ... --> to fulfill your post length needs. Also, I would suggest using a # markdown or HTML header tag to start your post with a header. \$\endgroup\$ – Jonathan Frech Sep 21 '18 at 1:18
  • 1
    \$\begingroup\$ Rather than adding markup comments, consider adding an explanation to your answer on how your program works. This will improve the quality of your answers and make it more likely that it will attract upvotes. \$\endgroup\$ – mbomb007 Sep 21 '18 at 1:29
0
\$\begingroup\$

MathGolf, 1 byte

§

Try it online!

Finally a showcase for the implicit input! Works regardless of the order of the input (if the string or int comes first).

\$\endgroup\$

Your Answer

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

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