替换空格算法
2014-01-20 13:56
218 查看
#include "stdio.h" #include "string.h" #include <stdlib.h> char* change(char *str) { //判断有多少空格 int i=0; char *p=str; while(*p) { if((*p)==' ')i++; p++; } //申请新空间 char *temp=(char *)malloc(strlen(str)+3*i+1); //开始置换 p=str; int k=0; while((*p)) { if((*p)!=' ') { temp[k]=(*p); } else { temp[k]='%';k++; temp[k]='2';k++; temp[k]='0'; } p++; k++; } temp[k]='\0'; return temp; } int main() { char* out="i am a boy!"; out=change(out); printf("%s\n",out); return 0; }
将空格替换成为“%20”的算法
相关文章推荐
- 字符串替换空格算法
- 面试算法(三)替换空格
- 剑指offer,时间复杂度为o(n)的空格替换算法,44页例子代码
- 算法--空格替换
- 字符串中空格替换为%20(算法)
- 算法练习二:替换空格
- 算法:替换字符串中的空格
- [PHP]算法-替换空格的PHP实现
- 字符串的空格替换算法
- 算法题目--替换空格
- [面试] 算法(五)—— 替换字符串中的空格
- 算法之每日一题:替换字符串空格
- 字符串替换空格的最优算法
- 每天一道算法题——替换空格
- 算法题目---替换空格
- 替换空格算法分析
- 算法学习之旅,初级篇(19)--替换空格
- 算法学习记录二(C++)--->字符串空格替换
- 每天一个算法练习之字符空格替换
- 跟我一起学算法系列4---替换空格