您的位置:首页 > 编程语言 > Python开发

[python]反转英文句子

2017-08-05 16:50 197 查看
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