输入:we are happy 输出we%20are%20happy
要求是原位移动,不申请新的空间
void replaceSpace(char *pStr,int nSize)
{
int nLen=strlen(pStr);
int nCnt=0;
for(int i=0;i<nLen;i++)
{
if (pStr[i]==' ')
{
nCnt++;
}
}
int nLenObj=nLen+2*nCnt;
if (nSize<nLenObj+1)
{
cout<<"空间不足"<<endl;
return;
}
for (int i=nLen-1;i>=0;--i)
{
if (pStr[i]==' ')
{
nCnt--;
pStr[i+nCnt*2]='%';
pStr[i+nCnt*2+1]='2';
pStr[i+nCnt*2+2]='0';
continue;
}
pStr[i+nCnt*2]=pStr[i];
}
for (int i=0;i<nLenObj;i++)
{
cout<<pStr[i];
}
cout<<endl;
}