#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
void repace_black(char *str)
{
assert(str);
char *pstr = str;
int black = 0;
int len = 0;
int newlen = 0;
while (*str)
{
if (*str == ‘ ‘)
black++;
len++;
str++;
}
newlen = len + black * 2;
char *newptr = pstr + newlen - 1;
char *oldptr = pstr + len - 1;
while (oldptr < newptr)
{
if (*oldptr == ‘ ‘)
{
*newptr-- = ‘0‘;
*newptr-- = ‘2‘;
*newptr-- = ‘%‘;
}
else
{
*newptr-- = *oldptr;
}
oldptr--;
}
}
int main()
{
char str[20] = "we are happy.";
repace_black(str);
printf("%s\n", str);
system("pause");
return 0;
}
字符串替换空格:请实现一个函数,把字符串中的每个空格替换成“%20。
原文地址:http://10324470.blog.51cto.com/10314470/1687594