题目:请实现一个函数,把字符串中每个空格替换成"%20"。例如输入“We are happy.”,则输出"We%20are%20happy."。
#include "iostream"
#include "string"
using namespace std;
#define MAXSIZE 100
void ReplaceBlank(char* str)
{
int len,length,count =0,index =0;
char* p1,*p2;
while(str[index] !='\0')
{
if (str[index] ==' ')
{
count++;
}
index++;
}
length =index+count*2;
p2 =str +length;
p1 =str+strlen(str);
while(p1!=str)
{
if (*p1 ==' ')
{
p1--;
*(p2--) ='0';
*(p2--) ='2';
*(p2--) ='%';
}
else
{
*(p2--) =*(p1--);
}
}
if (*p1 ==' ')
{
*(p2--) ='0';
*(p2--) ='2';
*(p2--) ='%';
}
}
void main()
{
char array[MAXSIZE] ={"We are happy! huang tao!"};
ReplaceBlank(array);
cout<<array<<endl;
}