题目:请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
代码:
s = input()
# write code here
str=''
for i in s:
if (i==" "):
str=str+ "%20"
else:
str=str+i
print(str)
剑指offer代码:
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
str=''
for i in s:
if (i==" "):
str=str+ "%20"
else:
str=str+i
return(str)