题目描述:
请实现一个函数,将一个字符串中的空格替换成“ % 20”。例如,当
字符串为We Are Happy.则经过替换之后的字符串为We % 20Are % 20Happy。
void replaceSpace(char str[], int Length) {
assert(str != NULL || Length > 0);
int num=0;
int length = 0;
int i = 0;
while ('\0' !=str[i])
{
++length;
if (str[i] ==' ')
{
num++;
}
i++;
}
int NewLength = 2 * num + length;
int PrevofArray = length;
while (PrevofArray>=0 && NewLength >PrevofArray)
{
if (str[PrevofArray] == ' ')
{
str[NewLength--] = '0';
str[NewLength--] = '2';
str[NewLength--] = '%';
}
else
{
str[NewLength--] = str[PrevofArray];
}
--PrevofArray;
}
}
本文介绍了一个函数实现,用于将字符串中的空格字符替换成“%20”。通过遍历原始字符串来计算空格数量,并预留足够的空间存放转换后的字符串。然后从字符串末尾开始反向遍历并替换。
1678

被折叠的 条评论
为什么被折叠?



