14
\$\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
  • 16
    \$\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

69 Answers 69

1 2
3
0
\$\begingroup\$

Husk, 1 byte

Another built-in, 1-indexed:

!

Try it online!

| improve this answer | |
\$\endgroup\$
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!

| improve this answer | |
\$\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

| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Rebol, Red 4 bytes

s/:n

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

| improve this answer | |
\$\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
  • 2
    \$\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).

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Acc!!, 85 bytes

N
Count i while _/128^i/32 {
_+128^(i+1)*N
}
Count j while N/48 {
_/128
}
Write _%128

Takes the string on one line of stdin and the 0-based index, in unary, on the following line. (I hope unary is okay. Decimal I/O is a pain in Acc!!) Try it online!

Commented

# Read printable ASCII characters into the accumulator, storing the first character
# in the least significant place
N
Count i while _/128^i/32 {
_+128^(i+1)*N
}
# Read 1s, shifting the accumulator down by one character's worth each time
Count j while N/48 {
_/128
}
# We have now removed the first i characters from the original input string,
# which means the first remaining character is the one at index i (0-based)
Write _%128
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -plF, 9 bytes

$_=$F[<>]

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Befunge-98 (FBBI), 5 bytes

1-indexing. Input is the index, a space and the string.

&k~,@

Try it online!

This just reads index characters as input and prints the last one.

| improve this answer | |
\$\endgroup\$
1 2
3

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.