请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy
#include<iostream>
using namespace std;
class RE{
public:
void Replace(char*str,int length){
int i=0;
int intputle=0;
int count=0;
while(str[i]!='\0'){
intputle++;
if(str[i]==' '){
count++;
}
i++;
}
int outputle=intputle+count*2;
while(intputle>=0&&outputle>intputle){
if(str[intputle]==' '){
str[outputle--]='0';
str[outputle--]='2';
str[outputle--]='%';
}
else{
str[outputle--]=str[intputle];
}
intputle--;
}
}
};
int main(){
RE re;
const int length=100;//定义为常量
char str[length]="we are happy";
cout<<str;
cout<<"===============================";
re.Replace(str,length);
cout<<str;
return 0;
}