替换字符串中的空格
2016-05-29 21:35
288 查看
题目:实现一个函数,要求吧字符串中的所有空格替换成“%20”。
例如“we are happy."-->”we%20are%20happy."
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/75/43/wKioL1Y0QMzy4FWPAABNfON8JjY737.jpg" title="{J9B4LEIWHTPYPA2Z8$NC)U.png" alt="wKioL1Y0QMzy4FWPAABNfON8JjY737.jpg" />
例如“we are happy."-->”we%20are%20happy."
#include <stdio.h> #include <stdlib.h> #include <assert.h> void strReplace(const char *dst, char *pun, const char *src); int countSpace(const char *dst); int main() { char dst[] = "I am a student."; char src[] = "%520"; int space = countSpace(dst); int len = strlen(dst) + (strlen(src) - 1)*space; char *pun = (char *)malloc(len); strReplace(dst, pun, src); puts(pun); system("pause"); return 0; } /*求字符串中空格的个数*/ int countSpace(const char *dst) { int count = 0; while (*dst) { if (*dst == ' ') { count++; } dst++; } return count; } /*“we are happy.”“we%20are%20happy.”*/ void strReplace(const char *dst, char *pun, const char *src) { assert(dst); assert(src); while (*dst != '\0') { while (*dst != '\0' && *dst != ' ') { *pun = *dst; pun++; dst++; } char *start = src; if (*dst == ' ') { while (*src) { *pun = *src; pun++; src++; } src = start; dst++; } } *pun = '\0'; }运行代码:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/75/43/wKioL1Y0QMzy4FWPAABNfON8JjY737.jpg" title="{J9B4LEIWHTPYPA2Z8$NC)U.png" alt="wKioL1Y0QMzy4FWPAABNfON8JjY737.jpg" />
相关文章推荐
- 一组数中,只有两个数只出现了奇数次,其他所有数都是成对出现的,请找出那两个数
- 冒泡排序函数,可以给任意类型的数据排序,比如整形数组,字符数组,字符串数组等
- 快速排序函数的比较函数
- 二叉树 的先序 中序、后序遍历、层次遍历以及树状打印等操作
- C语言利用结构体实现一个通讯录
- C语言的注释与C++注释的转换
- 两个链表的合并
- 单链表的操作
- 单继承与多继承中的虚函数表和虚函数指针
- C++虚继承中的对象内存布局
- 大道至简阅读笔记01
- shell(十一)切割文件
- [DP] BZOJ 1270 [BeijingWc2008]雷涛的小猫
- HttpClient get请求在HttpResponse中无法获得Location的问题
- 循环
- 天梯匹配系统 - 简单实现
- [平衡树动态维护凸包] BZOJ 2300 [HAOI2011]防线修建
- hdu 4607(树的直径)
- 学习进度条13
- C语言编程经验总结