xxxxxxxxxx
string = 'hello people of india'
words = string.split() #converts string into list
print(words[::-1])
xxxxxxxxxx
# in order to make a string reversed in python
# you have to use the slicing as following
string = "racecar"
print(string[::-1])
xxxxxxxxxx
#linear
def reverse(s):
str = ""
for i in s:
str = i + str
return str
#splicing
'hello world'[::-1]
xxxxxxxxxx
# Recursive
def reverse(string):
if len(string) == 0:
return string
else:
return reverse(string[1:]) + string[0]
# Iterative
def reverse_iter(string):
if len(string) == 0:
return string
else:
new_string = ""
for i in range(len(string)):
new_string += string[len(string)-i-1]
return new_string