python 反转字符串中的单词

1、问题描述:

you are student    ------>结果为:student are you, 考虑不使用split和reverse 方法

2、 思路:

按照单词逆序,然后整个字符串逆序。

input your words:you are student
First change:uoy era tneduts
Second change:student are you

3.话不投机,直接上代码:

# 交换
def swap(str1,begin,to):
    temp = str1[begin]
    str1[begin]= str1[to]
    str1[to]=temp

#字符串反转
def turnover(lists, begin,to):
    while(begin

上面这种方式通用对于前后有空格的情况可以处理。 

input your words: you are a student
First change: uoy era a tneduts
Second change:student a are you 

input your words:you are a student 
First change:uoy era a tneduts 
Second change: student a are you

 

你可能感兴趣的:(数据结构和算法)