「世界で闘うプログラミング力を鍛える本」Pythonコード解答例 - 1.3 URLify
#目次
###CHAP1. 配列と文字列
#Pythonコード解答例
def replaceSpaces(str,trueLength):
spaceCount = 0
for i in range(trueLength):
if str[i] == " ":
spaceCount = spaceCount + 1
index = trueLength + spaceCount * 2
target = [0] * index
for i in range(trueLength-1,-1,-1):
if str[i] == " ":
target[index-1] = "0"
target[index-2] = "2"
target[index-3] = "%"
index = index - 3
else:
target[index-1] = str[i]
index = index - 1
return "".join(target)
input_str = "Mr John Smith"
input_num = len(input_str)
print(replaceSpaces(input_str,input_num))