当我返回 .join 方法来反转字符串时,我调用函数本身来这样做。如何在不使用内置 Python 方法(例如 reversed 方法)的情况下反转此字符串。
def reverse_string_3(string):
length = len(string)
spaces = [' ']
words = []
index_tracker = 0
while index_tracker < length:
if string[index_tracker] not in spaces:
beginning_of_word = index_tracker
# we only want to increment when the index tracker is a letter and not spaces
while index_tracker < length and string[index_tracker] not in spaces:
index_tracker += 1
words.append(string[beginning_of_word:index_tracker])
index_tracker += 1
return "".join(reverse_string_3(string))
侃侃无极
相关分类