《剑指offer》面试题4(替换空格)
来源:互联网 发布:网络风靡的性感骚彤彤 编辑:程序博客网 时间:2024/05/29 07:17
#include <iostream>#include <cstdio>#include <cstring>using namespace std;void replaceBlank(char* str);int main(){ char* str; str = new char[100]; strcpy(str," str str "); replaceBlank(str); cout<<str<<endl; strcpy(str,"str"); replaceBlank(str); cout<<str<<endl; replaceBlank(NULL); //cout<<str<<endl; strcpy(str,""); replaceBlank(str); cout<<str<<endl; strcpy(str," "); replaceBlank(str); cout<<str<<endl; strcpy(str," "); replaceBlank(str); cout<<str<<endl; return 0;}void replaceBlank(char* str){ if(str == NULL) return; int length = strlen(str); int blankcount = 0; for(int i=0;i<length;i++) if(str[i] == ' ') blankcount++; int newlength = length + 2*blankcount; str[newlength] = '\0'; int p = newlength-1; for(int i = length-1;i>=0;i--) { if(str[i] == ' ') { str[p--] = '0'; str[p--] = '2'; str[p--] = '%'; } else str[p--] = str[i]; }}/*char* replaceBlank(const char* str) //创建新的内存空间{ if(str == NULL) return NULL; //strlen()函数的参数不能是NULL int length = strlen(str); char* newstr = new char[length*3+1]; int p = 0; for(int i=0;i<length;i++) { if(str[i] == ' ') { newstr[p++] = '%'; newstr[p++] = '2'; newstr[p++] = '0'; } else newstr[p++] = str[i]; } newstr[p] = '\0'; return newstr;}*/
0 0
- 剑指offer-->面试题4 替换空格
- 《剑指offer》面试题4 替换空格
- 【剑指offer】面试题4:替换空格
- 【剑指offer】面试题4替换空格
- 剑指offer 面试题4 替换空格
- 剑指Offer:面试题4 替换空格
- 剑指offer-面试题4-替换空格
- 《剑指Offer》面试题4:替换空格
- 【剑指offer】 面试题4: 替换空格
- 剑指offer 面试题4---替换空格
- 剑指offer面试题4-- 替换空格
- 【剑指offer】面试题4:替换空格
- 剑指Offer:面试题4 替换空格
- 剑指offer-面试题4:替换空格
- 剑指Offer 面试题4 替换空格
- 剑指offer-----面试题4(替换空格)
- 剑指offer面试题4 替换空格
- 剑指offer面试题4:替换空格
- 应届毕业生三方协议
- 网络编程socket基本API详解
- ios视图切换效果
- 《剑指offer》面试题4(合并数组)
- 黑马程序员--Java异常处理
- 《剑指offer》面试题4(替换空格)
- linux 常用命令
- POJ 2536 Gopher II(二分图最大匹配)
- 《剑指offer》链表的结点的插入删除
- Socket 多线程
- 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问
- Java web之mysql数据源
- 八千里路云和月
- DataTable填充实体类返回泛型集合