def reverse(li, start, wl):
i, j = start, start + wl - 1
while i < j:
li[i], li[j] = li[j], li[i]
i += 1
j -= 1
def fan(s):
li = list(s)
llen = len(li)
start = 0
while (start < llen) and (li[start] == ' '):#第一个单词开始的位置
start += 1
while start < llen: #开始的位置
wl = 0
while ((start + wl) < llen) and (li[start + wl] != ' '):#将单词的长度找出来
wl += 1
reverse(li, start, wl)
start += wl
while (start < llen) and (li[start] == ' '):
start += 1
li.reverse()
return ''.join(li)
s = " hello world "
rs = fan(s)
print(rs)
[python]反转英文句子
最新推荐文章于 2023-06-04 03:20:58 发布