标签:ros sub span highlight rcp style mic har describe
题目描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
法一:C++实现
class Solution { public: void replaceSpace(char *str,int length) { int len = strlen(str); string ans; for(int i = 0; i < len; ++i){ if(str[i] == ‘ ‘) ans += "%20"; else ans += str[i]; } strcpy(str, ans.c_str()); } };
法二:Java 实现
public class Solution { public String replaceSpace(StringBuffer str) { return str.toString().replace(" ", "%20"); } }
法三:python实现
# -*- coding:utf-8 -*- class Solution: # s 源字符串 def replaceSpace(self, s): # write code here return s.replace(" ", "%20");
标签:ros sub span highlight rcp style mic har describe
原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/12273195.html