剑指offer面试题4-- 替换空格
来源:互联网 发布:淘宝论坛帐号 编辑:程序博客网 时间:2024/05/16 05:05
#include <iostream>using namespace std;class Solution {public: void replaceSpace(char *str, int length) { if (NULL == str || length <= 0) { return; } int newLength = 0, oldLength = 0;; int index = 0; newLength = strlen(str); oldLength = strlen(str); while (str[index] != '\0') { if (str[index] == ' ') { newLength += 2; } index++; } if (newLength > length) { return; } while (oldLength >= 0) { if (str[oldLength] == ' ') { str[newLength--] = '0'; str[newLength--] = '2'; str[newLength] = '%'; } else { str[newLength] = str[oldLength]; } newLength--; oldLength--; } }};int main(){ Solution s; char str[100] = "we are happy."; s.replaceSpace(str, 100); cout << str << endl << "len" << strlen(str) << endl;; system("PAUSE"); return 0;}
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:替换空格
- Trie树 hdu4825 Xor Sum
- 随机数、唯一字符串
- HDOJ Guess the number 3337【神题-抓取杭电后台输出数据】
- JAVA异常之spring 4.2.0兼容asm报.ClassVisitor as super clas
- IO流
- 剑指offer面试题4-- 替换空格
- 聊天式对话框布局问题match_parent和layout_gravity那些事(android点点滴滴)
- QT仿酷狗
- 浅析Android Context
- iOS UITableView数据刷新
- STL hdu1540 Tunnel Warfare
- codeforces Round 314 div.2
- 什么是Windows服务
- 关于IO的同步,异步,阻塞,非阻塞