4--替换空格
2015-09-27 18:52
218 查看
/* 题目要求: 替换空格。 we are happy。 we%20are%20happy。 算法解析: 字符串长度为14. 先计算有多少个空格,测试字符串为2个。这样总长度为18. 从最后一个字符向后移动,注意控制指针,空格是1个字符,%20是三个。 */ #include <stdio.h> void replaceStr(char str[]) { int count_space = 0; int str_length = 0; for (int i = 0; str[i] != '\0'; i++) { if (str[i] == ' ') count_space++; str_length++; } // printf("%d\n",count_space); int p1 = str_length; int p2 = str_length + count_space * 2; printf("%d, %d\n", p1, p2); while (p1 != p2 || p1 < 0) { while (str[p1] != ' ') { str[p2--] = str[p1--]; } str[p2--] = '0'; str[p2--] = '2'; str[p2--] = '%'; p1--; } } int main() { char str[30] = "we are happy."; replaceStr(str); printf("%s\n", str); return 0; }
相关文章推荐
- 测试基础知识
- 一步一步的理解C++STL迭代器
- BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )
- 3--合并数组
- 黑马程序员-----c学习中的小程序
- ThinkPHP--控制器调用模板
- hdu5478Can you find it
- 2--判断该数组中是否存在这个整数
- DirectX11 XNA数学库之向量
- Java虚拟机8:虚拟机性能监控与故障处理工具
- eclipse导入web项目之后会报一大堆错误
- vim-projectionist插件
- 关于ServletOutputStream源代码引发的探究
- warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失.
- Decode Ways
- 1--字符串转为整形
- 设置背景
- Android菜鸟还是当走一点,学一点罢(对基础的学习万不可松懈)
- 解决跨域访问
- Vim扫盲: buffer,window,tab