写一个函数,将字符串中空格替换为%20。

写一个函数,将字符串中空格替换为%20。样例:"abc defgx yz"替换为"abc%20defgx%20yz"。
这道题是一道简单的字符和字符串替换题,字符的替换直接用指针即可,每次都需要把空格后的字符串保存到一个数组中,然后把空格替换为%20后再将刚刚拷贝的字符串拷贝到%20的后面,代码如下:


Fun(char str)
{
char
p = str;
char arr[20];
while (p != '\0')
{
if (
p == ' ')
{
strcpy(arr, p + 1);
p = '%';
(p + 1) = '2';
*(p + 2) = '0';
strcpy(p + 3, arr);
p = p + 3;
continue;
}
p++;
}
}

猜你喜欢

转载自blog.51cto.com/14239789/2429505
今日推荐