class Solution: def replaceSpace(self, s: str) -> str: res=[] for c in s: if c==' ': res.append("%20") else: res.append(c) return "".join(res)
#include<string> class Solution { public: string replaceSpace(string s) { string res=""; for(int i=0;i<s.length();i++){ if(s[i]==' '){ res+="%20"; //strcat(res,"%20"); } else{ res+=s[i]; //strcat(res,s[i]); } } return res; } };
注意:'\0'和' '输出在屏幕上是一样的,但ascii code不同,'\0'代表字符串的结束