0
How to fix this
I want to create a program which reverses a string using recursion. Why the error is happening and how to fix it? https://code.sololearn.com/cX8UuyP6a5u8/?ref=app
3 Answers
+ 4
Anonymous Person On line number 6 you have passed number as parameter which doesn't has len()
rev(n-1) //wrong
Change rev(n - 1) to rev(string[:n])
https://code.sololearn.com/ccf9QK7HSJwb/?ref=app
0
Here is the simple way,
def rev(string):
if len(string) == 0:
return ""
return (string[-1] + rev(string[:-1]))
print(rev("news"))