天天看點

劍指offer——2

題目描述

請實作一個函數,将一個字元串中的每個空格替換成“%20”。例如,當字元串為We Are Happy.則經過替換之後的字元串為We%20Are%20Happy。  

# -*- coding:utf-8 -*-
class Solution:
    # s 源字元串
    def replaceSpace(self, s):
        # write code here
        n=len(s)
        for i in range(n):
            if s[i]==' ':
                s=s.replace(s[i],'%20')
        return s
        
while 1:
    try:
        ss=Solution()
        s=raw_input()
        print(ss.replaceSpace(s))
    except:
        break      

轉載于:https://www.cnblogs.com/yangyang1989/p/11402574.html