CC BY 4.0 (除特别声明或转载文章外)
如果这篇博客帮助到你,可以请我喝一杯咖啡~
替换空格
#include<bits/stdc++.h>
using namespace std;
void ReplaceBlank(char string[],int length)
{
if(string==nullptr||length<=0) return;
int originalLength=0;
int numberOfBlamk=0;
int i=0;
while(string[i]!='\0')
{
++originalLength;
if(string[i]=' ') ++numberOfBlank;
++i;
}
int newLength=originalLength+numberOfBlank*2;
if(newLength>length) return ;
int indexOfOriginal=originalLength;
int indexOfNew=newLength;
while(indexOfOriginal>=0 && indexOfNew>indexOfOriginal)
{
if(string[indexOfNew]==' ')
{
string[indexOfNew--]='0';
string[indexOfNew--]='2';
string[indexOfNew--]='%';
}
else
{
string[indexOfNew--]=string[indexOfOriginal];
}
--indexOfOriginal;
}
}