将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
std::string replaceSpace(std::string &str){
int pos = 0;
while (pos != -1){
pos = str.find(' ', pos);
if (pos == -1){
break;
}
str.replace(pos, 1, "%20");
}
return str;
}
int main()
{
std::string str = "We are happy";
std::string new_str = replaceSpace(str);
std::cout << new_str << std::endl;
return 0;
}