牛客网编程题——字符串_空格替换

题目描述

请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。

给定一个string iniString 为原始的串,以及串的长度 int len, 返回替换后的string。

测试样例:

"Mr John Smith”,13
返回:"Mr%20John%20Smith"
”Hello  World”,12
返回:”Hello%20%20World”

用python很简单


# -*- coding:utf-8 -*-
class Replacement:
    def replaceSpace(self, iniString, length):
        # write code here
        res = ""
        for i in range(length):
            if iniString[i] == ' ':
                res += "%20"
            else:
                res += iniString[i]
        return res
发布了422 篇原创文章 · 获赞 256 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/qq_32146369/article/details/104934123