C++字符串空格替换题
在网络编程中,需要将URL参数中含有的特殊字符通过在'%'后加上ASCII码的两位十六进制的方法,转换成服务器端能够识别的字符,如空格的ASCII码为32即16进制的0x20,则需要替换为"%20"。
题目:请实现一个函数,把传入char*字符串中的每个空格替换成"%20",例如输入"We are happy.",则输出"We%20are%20happy."
时间复杂度为O(n)的解法思路:
首先遍历字符串,得到字符串长度originalLength并统计空格数numberOfBlank,可计算出替换后的字符串长度newLength = originalLength+2*numberOfBlank;
从字符串的尾部开始复制并向前移动,用两个下标IndexOfOriginal和IndexOfNew分别标记目前所指的原字符位置和替换后的字符位置;
当IndexOfOriginal指向空格时,IndexOfNew下标所指的字符及其往前1、2个位置分别替换为'0'、'2'、'%',替换完成后IndexOfOriginal向前移动一格,IndexOfNew相应向前移动3格,否则都往前移动一格;
最后在字符串尾部即下标为newLength-1的位置添加'\0'。
代码:
void ReplaceBlank(char str[], int maxLength) {if(str == NULL || maxLength <=0)return;//calculate the new length after replacing and number of Blanksint originalLength = 0;int numberOfBlank = 0;int index = 0;for(index = 0; str[index] != '\0'; index++) {if(str[index] == ' ')++numberOfBlank;}originalLength = index+1;//if the number of blank is 0, nothing changesif(numberOfBlank==0)return;//if the new length exceeds the max lengthint newLength = originalLength+2*numberOfBlank;if (newLength > maxLength) {cout << "new Length exceeds the max Length!" << endl;return;}//start at the character before '\0'int indexOfOriginal = originalLength-2;int indexOfNew = newLength-2;while (indexOfOriginal>=0 && indexOfNew > indexOfOriginal) {if (str[indexOfOriginal] == ' ') {str[indexOfNew--] = '0';str[indexOfNew--] = '2';str[indexOfNew--] = '%';} else {str[indexOfNew--] = str[indexOfOriginal];}--indexOfOriginal;}//add a '\0' at laststr[newLength-1] = '\0'; }
完整代码:
#include <iostream> #include <stdio.h> using namespace std;void ReplaceBlank(char str[], int maxLength) {if(str == NULL || maxLength <=0)return;//calculate the new length after replacing and number of Blanksint originalLength = 0;int numberOfBlank = 0;int index = 0;for(index = 0; str[index] != '\0'; index++) {if(str[index] == ' ')++numberOfBlank;}originalLength = index+1;//if the number of blank is 0, nothing changesif(numberOfBlank==0)return;//if the new length exceeds the max lengthint newLength = originalLength+2*numberOfBlank;if (newLength > maxLength) {cout << "new Length exceeds the max Length!" << endl;return;}//start at the character before '\0'int indexOfOriginal = originalLength-2;int indexOfNew = newLength-2;while (indexOfOriginal>=0 && indexOfNew > indexOfOriginal) {if (str[indexOfOriginal] == ' ') {str[indexOfNew--] = '0';str[indexOfNew--] = '2';str[indexOfNew--] = '%';} else {str[indexOfNew--] = str[indexOfOriginal];}--indexOfOriginal;}//add a '\0' at laststr[newLength-1] = '\0'; }void test(char str[], int maxLength) {cout << "-----------test-------------" << endl;printf("Original: %s\n", str);ReplaceBlank(str, maxLength);printf("Replaced: %s\n", str);cout << "-----------end---------------" << endl;} int main() {/*char str[50] = "hello world and s";ReplaceBlank(str, 50);printf("%s\n", str);*/char str1[50]="hello world and s";char str2[50]="";char str3[20]="hello world! fdsjkd";char str4[50]=" helloweroddfsd";char str5[50]=" ";test(str1, 50);test(str2, 50);test (NULL, 50);test(str3, 20);test(str4, 50);test(str5, 50);return 0; }
参考资料:《剑指Offer名企面试官精讲典型编程题》
转载于:https://www.cnblogs.com/RDaneelOlivaw/p/7366577.html
C++字符串空格替换题相关推荐
- 剑指offer笔记(六)字符串空格替换
字符串空格替换 题目:实现一个函数,把字符串中的每一个空格替换为"%20",eg:we are happy 替换为 we%20are%20happy 由原来一个空格字符,替换之后 ...
- 剑指offer系列-----item3字符串空格替换
题干: 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 分析: 这题 ...
- java字符串下标替换_字符串空格替换成指定元素实例思路讲解java
请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20lucky. 思路:从前向后记录' ...
- LeetCode/LintCode 题解丨一周爆刷字符串:空格替换
描述 设计一种方法,将一个字符串中的所有空格替换成 %20 . 字符串以字符数组的形式给出,你可以假设该字符串有足够的空间来加入新的字符,且你得到的是"真实的"字符长度. 你的程序 ...
- c语言实现字符串空格的替换--详细
题目:请实现一个函数,把字符串中的每个空格替换成"%20".例如,输入"We are happy.", 则输出"We%20are%20happy.&q ...
- 字符串系列② -- 替换空格
目录 题目 思路 代码实现 方法二的实现 StringBuffer的实现方法 数组填充类算法问题的思考 题目 此题对应LeetCode中剑指offer05 请实现一个函数,把字符串 s 中的每个空格替 ...
- c语言中空格字符怎么表示_漫画:腾讯面试题,请实现把字符串中的空格替换为“%20”...
面试现场 题目描述请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. i ...
- 替换字符串空格 - Java - StringBuffer
问题描述: 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 基本思路: ...
- 剑指offer二:字符串中的空格替换
题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 我的思路: ...
最新文章
- HTTP访问服务的相关解释
- 程序员,勿让岁月偷走你年轻的秀发!
- Dockerfile实践优化建议
- Http 协议详解笔记
- 用看门狗定时器做计时器,计算程序耗时,超声波测距,FL2440
- KNN与K-Means
- 深度?广度?浅析技术人员的职业发展之路
- oracle 执行计划
- boost官方文档同步机制Synchronization mechanisms overview
- JMS Helloworld
- 云服务器是什么,有什么用?
- 在不改变链表的情况下从尾到头打印连表
- 1.2.3 TCP/PI参考模型(应用层、传输层、网际层、网络接口层)、五层参考模型(应用层、传输层、网络层、数据链路层、物理层)、OSI与TCP/IP参考模型比较(转载)
- python入门经典100例-【python】编程语言入门经典100例--22
- 19. 网购秒杀系统架构分析
- mysql必知必会的数据_MySQL必知必会---数据过滤
- android 应用市场 审核速度,安卓市场上传APP软件要多长时间审核?
- Clamav使用及规则库详解
- xp系统什么梗_电脑分区4K对齐,对系统的影响
- 【C语言】如何将函数内部申请的内存,放到函数外部也能引用到