Here is how to reverse a string using recursive function in Python.
Run the code here: https://repl.it/@VinitKhandelwal/reverseString
.ecnetnes rehtonA
Run the code here: https://repl.it/@VinitKhandelwal/reverseString
def reverseStringIteration(sentence):
reversed_string = ""
for i in range(0, len(sentence)):
reversed_string += sentence[-1]
sentence = sentence[:-1]
return reversed_string
def reverseStringRecursion(sentence):
if sentence == "":
return ""
else:
return reverseStringRecursion(sentence[1:])+sentence[0]
print(reverseStringRecursion("This is a sentence."))
print(reverseStringIteration("Another sentence."))
OUTPUT
.ecnetnes a si sihT.ecnetnes rehtonA
Comments
Post a Comment