题目描述
- 请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
解题思路
- 无
AC代码
class Solution {
public:
void replaceSpace(char *str,int length) {
char* aux = (char*)malloc(length);
const char* s = "%20";
for(int i = 0; i< length; i++)
aux[i] = str[i];
int p1 = 0, p2 = 0;
while(p1 < length)
{
if(aux[p1] == ' ')
{
for(int i = 0; i < 3; i++)
str[p2++] = s[i];
}
else
str[p2++] = aux[p1];
p1++;
}
}
};
网友评论