简单题
这是一道简单的字符串题,考验的是对字符串最基本的操作。
class Solution {
public:
void replaceSpace(char *str,int length) {
char temp[100] = {};
while(*str)
{
if(*str == ' ')
{
strcpy(temp,str+1);
strcpy(str,"%20");
strcat(str,temp);
}
str++;
}
}
};
从字符串第一个字符取起,遍历至字符为’\0’为止,遇’ ‘则将后面的字符串保存至temp中,将"%20"拷贝至指针当前所指的位置,随后再把之前存放至temp中的字符串拷贝至末尾,这样就完成了将’ '替换成"%20"的操作,指针遍历至末尾即完成了对整个字符串中空格的替换。