剑指offer面试题4
来源:互联网 发布:知乎 段子 编辑:程序博客网 时间:2024/05/29 18:33
题目:实现一个函数,把字符串中的每个空格替换成“%20”,例如输入“we are happy”,输出“we%20are%20happy”。
我自己试着也写了一下,欢迎大家指正。
#include <iostream>void replaceBlank(char *src, int len , char *dst , int new_len){ // 这里减1是因为最后一个是'\0' for (int i = len - 1; i >= 0 ; --i) { if (src[i] == ' ') { dst[--new_len] = '0'; dst[--new_len] = '2'; dst[--new_len] = '%'; } else { dst[--new_len] = src[i]; } }}void main(){ char *src = "we are happy"; std::cout << src << std::endl; int len = strlen(src) + 1; /*----------------------------------*/ // 计算空格数 int blank_num = 0; for (int i = 0 ; i < len ; ++i) { if (src[i] == ' ') { blank_num++; } } int new_len = len + 2 * blank_num; char *dst = new char[new_len]; replaceBlank(src, len, dst , new_len); std::cout << dst << std::endl;}
阅读全文
0 0
- 剑指offer面试题4
- 剑指offer 面试题4
- 【面试题】剑指offer 4
- 剑指offer面试题4
- 剑指offer面试题4
- 剑指offer面试题4
- 剑指offer 面试题
- 剑指offer面试题
- 剑指offer-->面试题4 替换空格
- 读书笔记-剑指offer面试题4
- 《剑指offer》面试题4 替换空格
- 【剑指offer】面试题4:替换空格
- 【剑指offer】面试题4替换空格
- 剑指offer 面试题4 替换空格
- 剑指Offer:面试题4 替换空格
- 剑指offer-面试题4-替换空格
- 《剑指Offer》面试题4:替换空格
- 《剑指Offer》面试题4习题
- 最大子数组
- linux常见考点
- 阶乘计算升级版
- 审视跨域请求实现方式
- 近期总结
- 剑指offer面试题4
- 权限系统--角色管理
- CSDN博客积分规则
- 创建mysql性能索引
- 对话框 适用于recyclerView点击Item中的按钮弹出
- 为什么要使用NoSQL
- 学习html第一周
- poj 3320 尺取法
- 常用详细的struts.xml和web.xml配置