编程题 将一个字符串中的每个空格替换成“%20”
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
void replaceSpace(char *str,int length) {
if( str==NULL || length<=0 ) return ;
std::string m_str="";
int k=0;
while( str[k] != '\0' )
{
if(str[k] != ' ')
{
char c = str[k];
//m_str.append(&c); // 这个地方的append() 不能用!!!!!!!!
m_str += c;
}
else
{
m_str.append("%20");
}
++k;
}
if( m_str.length()+1 > length )
return;
int i=0;
for(; i<m_str.length(); ++i)
{
str[i] = m_str[i];
}
str[i] = '\0';
}
};
int main()
{
char str[20]= {'h', ' ','e','\0'};
Solution s;
s.replaceSpace(str, 20);
cout << str << endl;
return 0;
}