实现一个函数,将一个字符串中的空格替换成“%20”
#!/usr/bin/env python
# -*- coding: utf-8 -*-
test = raw_input() # 从键盘输入
inital_length = len(test)
kong = 0 # kong 用来统计输入的一行数据中总共有多少个空格
for i in test:
if i ==" ":
kong += 1
final_length = inital_length + 2*kong # 计算转换后的数组的长度
p1 = inital_length - 1
p2 = final_length - 1
result = [0]*final_length # 预先设定一个新的数组
if p1 == p2:
print(test)
while p1 >= 0: # 重复此循环直到p1=0
if test[p1] != " ":
result[p2] = test[p1]
p1 = p1-1
p2 = p2 - 1
elif test[p1] == " ":
result[p2] = "0"
result[p2 - 1] = "2"
result[p2 - 2] = "%"
p1 = p1 - 1
p2 = p2 - 3
convert_str = ''.join(result) # ''.join()将一个数组转换为一个字符串
print(convert_str)