标签:
// 字符串替换空格:请实现一个函数,把字符串中的每个空格替换成“%20”。 // 例如输入“we are happy.”,则输出“we%20are%20happy.” #include <stdio.h> #include <assert.h> char* replace(char* p) { char* ret = p; int num = 0; int oldlen = 0; int newlen = 0; char* q = p; char* r; assert(p != NULL); while (*p != '\0') { if (*p == ' ') { num++; } oldlen++; p++; } p = q; newlen = oldlen + 2 * num; q = p + oldlen - 1; r = p + newlen - 1; while (q != r) { if (*q == ' ') { *r-- = '0'; *r-- = '2'; *r-- = '%'; } else { *r = *q; r--; } q--; } return ret; } int main() { char p[20] = "we are happy."; printf("原字符串是:%s\n", p); printf("替换后的字符串是:%s\n", replace(p)); return 0; } <img src="http://img.blog.csdn.net/20150704100218180?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
版权声明:本文为博主原创文章,未经博主允许不得转载。
【c语言】字符串替换空格:请实现一个函数,把字符串中的每个空格替换成“%20”
标签:
原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/46753613