For this challenge, a word is defined as a string of letters, separated by spaces.

Given a string of words, and an integer i representing an index into that string, output the whole word which contains the character at index i.

For example (using 0-based indexing), with the string code golf, and the index 6, the 7th character is the o (code golf), so the output is the word golf.

You may use 0- or 1-based indexing, and you may assume the index i is in range, and does not point to a space.

You may assume the string:

  • is non-empty
  • contains only lowercase ASCII letters and spaces
  • contains no consecutive spaces
  • does not start or end with a space

Test cases


index   string                   output
0       w                        w
6       a e i o u                o
6       code golf                golf
17      red lorry yellow lorry   lorry
8       avuncular                avuncular


Browse other questions tagged or ask your own question.