标签:
//字符串替换空格:实现一个函数,把字符串里的空格替换成“%20” #include <stdio.h> #include <assert.h> void replace(char *src) { assert(src); int OldLen = 0; //原字符串长度 int NewLen = 0; //新字符串长度 int BlackNum = 0; //空格数量 int NewBack = 0; //新字符串尾部 int OldBack = 0; //原字符串尾部 while (src[OldLen] != '\0') { if (src[OldLen] == ' ') { BlackNum++; } OldLen++; } NewLen = OldLen + BlackNum * 2; OldBack = OldLen-1; NewBack = NewLen - 1; while (OldBack!=0) { if (src[OldBack] == ' ') { src[NewBack--] = '0'; src[NewBack--] = '2'; src[NewBack] = '%'; } else { src[NewBack] = src[OldBack]; } OldBack--; NewBack--; } } int main() { char p[20] = "hello world !"; replace(p); printf("%s\n", p); return 0; return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”
标签:
原文地址:http://blog.csdn.net/doudouwa1234/article/details/46753439