刷题 ,当字符串为We Are Happy.则经过替换之后的字符串为We百分号20Are百分号20Happy。
C++ 空格替换带测试
// An highlighted block
#include <iostream>
#include <conio.h>
using namespace std;
class Solution {
public:
void replaceSpace(char *str,int length) {
int i=length,j=0,k=0;
char *temp =new char;
while(str==NULL)
{
//cout<<NULL;
}
while(i>0)
{
if(str[k]==' ')
{
temp[j]='%';
temp[j+1]='2';
temp[j+2]='0';
j=j+3;
}
else
{
temp[j]=str[k];
j++;
}
i--;
k++;
}
// temp[0]='1';
cout<<temp;
}
};
int main()
{
char *a="We Are Happy";
//cout<<a;
Solution b;
b.replaceSpace(a,13);
}