<p>试图解决一个问题,即我可以反转字符串中的每个单词,因为python中没有“\0”与C不同,我的逻辑无法提取字符串的最后一个字符。
你知道如何在不改变太多代码的情况下解决这个问题吗</p>
<pre><code>Input = This is an example
Output = sihT si na elpmaxe
import os
import string
a = "This is an example"
temp=[]
store=[]
print(a)
x=0
while (x <= len(a)-1):
if ((a[x] != " ") and (x != len(a)-1)):
temp.append(a[x])
x += 1
else:
temp.reverse()
store.extend(temp)
store.append(' ')
del temp[:]
x += 1
str1 = ''.join(store)
print (str1)
</code></pre>
<p>我的输出正在截断最后一个字符</p>
<pre><code>sihT si na lpmaxe
</code></pre>