标签:
#include <stdio.h> #include <string.h> #include <assert.h> char *Instead(const char *strSrc, char *strDes) { assert(strSrc != NULL && strDes != NULL); char *p=strDes; char *pDes; const char *pSrc; pDes = strDes; pSrc = strSrc; while (*pSrc != '\0') { if (*pSrc != ' ') { *pDes++ = *pSrc; //拷贝原字符串内容 } else { memcpy(pDes, "%20", 3); //遇到空格在替换后的那个字符数组中插入%20 pDes += 3; } pSrc++; } *pDes = '\0'; return p; } int main() { char s[] = "we are happy"; char s2[20]; char *p; p = Instead(s, s2); printf("%s",p); return 0; }
char* Instead1(char *str, int sz,int len) { char *pstr = str ; int blank_count = 0; int k; int point1, point2; while (*pstr) { if (*pstr == ' ' ) blank_count++; pstr++; } k = len + blank_count *2; point1 = len; point2 = k; str[k + 1] = '\0' ; while (point1 >= 0 && point2 >= point1) { if (str [point1] != ' ') str[point2--] = str [point1--]; else { str[point2--] = '0' ; str[point2--] = '2' ; str[point2--] = '%' ; point1--; } } return str ; } int main() { char s[20] = "we are happy" ; char *p; p = Instead1(s, sizeof(s)/sizeof (s[0]),strlen(s)); printf( "%s", p); return 0; }
博主初学小白,欢迎大家积极给出意见,一起讨论,一起进步。
标签:
原文地址:http://blog.csdn.net/bit_clearoff/article/details/51367255