[python]反转英文句子

xiaoxiao2021-02-28  123

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)
转载请注明原文地址: https://www.6miu.com/read-29273.html

最新回复(0)