代码:
#include<iostream>
#include<string>
#include<string>
using namespace std;
class Solution{
public:
string ReplaceBlank(string s)
{
int count=0;
int len = s.size();
for(char c:s)
{
if(c == ' ')
{
count++;
}
}
public:
string ReplaceBlank(string s)
{
int count=0;
int len = s.size();
for(char c:s)
{
if(c == ' ')
{
count++;
}
}
s.resize(len + 2*count);
for(int i = len-1,j = s.size()-1;i < j;i--,j--)
{
if(s[i]!=' ')
{
s[j] = s[i];
}
else
{
s[j-2] = '%';
s[j-1] = '2';
s[j] = '0';
j -= 2;
}
}
{
if(s[i]!=' ')
{
s[j] = s[i];
}
else
{
s[j-2] = '%';
s[j-1] = '2';
s[j] = '0';
j -= 2;
}
}
return s;
}
};
};
int main()
{
string s = "hello world !";
Solution sol;
std::cout << sol.ReplaceBlank(s) <<endl;
}
{
string s = "hello world !";
Solution sol;
std::cout << sol.ReplaceBlank(s) <<endl;
}
测试结果: