Here, the index of x given the string abcxcxyz is 2.

Python

#recursion

def index_of(str, idx=0) -> int:
    is_current_element = (len(str) == 1)
    current_element = str[-1]
    char = str[0]
    remaining_chars = str[1:]

    if is_current_element:
        return current_element
    
    if char == 'x':
        return idx

    return index_of(remaining_chars, idx+1)

assert index_of('abxcxyz') == 2