请实现—个函数,把字符串s中的每个空格替换成”%20""。
示例1:
输入:s = "we are happy ."输出:""Me%20are%20happy ."
#define _CRT_SECURE_NO_WARNINGS //vs2017下使用strcpy
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
void replaceSpace(string& tempStr)
{
char tempBuf[100] = { 0 };
strcpy(tempBuf, tempStr.c_str());
cout << tempBuf << endl;
tempStr.clear();
for (int i = 0; i < strlen(tempBuf); ++i)
{
if (tempBuf[i] == ' ')
{
tempStr.append("%20");
}
else
{
tempStr.push_back(tempBuf[i]);
}
}
}
int main()
{
string tempStr = "we are happy";
replaceSpace(tempStr);
cout << tempStr << endl;
}