Skip to content
Advertisement

Find reverse of string using recursive

I am writing recursive function, but it is not working. Here is my code,

def reverse(s):
    if not s:
        return ' '
    return reverse(s[1]) + s[0]

Advertisement

Answer

You need to tweak little logic as,

def reverse(s):
    if not s:
        return ''
    return reverse(s[1:]) + s[0]
Advertisement