请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;
string str = "We Are Happy.";
string str2 = "%20";
string str3 = " ";
int main(){
int pos;
while(str.find(str3) != string::npos){
pos = str.find(str3);
str.replace(pos, 1, str2);
}
cout << str << endl;
return 0;
}