请实现一个函数,把字符数组中的每个空格替换成“%20”。
例如输入“we are happy.”,则输出“we%20are%20happy.”。
#include<stdio.h>
#include<string.h>
void replace_space(char arr[])
{
int count_space=0;
int i=0;
char *star=arr+strlen(arr)-1;
char *end=arr+strlen(arr)-1;
while (arr[i])
{
if (arr[i]==' ')
{
count_space++;
}
i++;
}
end=end+count_space*2;
while (end!=star)
{
if (*star==' ')
{
*end='0';
end--;
*end='2';
end--;
*end='%';
star--;
end--;
}
else
{
*end=*star;
end--;
star--;
}
}
}
int main()
{
char arr[30]={"we are happy."};
replace_space(arr);
printf("%s\n",arr);
system("pause");
return 0;
}