// 替换空格
// 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”,
// 则输出“We%20are%20happy.”。 #include <iostream>
using namespace std; bool replace_space(char* str,const int length)
{
if (str == NULL && length <= )//上来先判断
return false; int true_length = , count = , i = ;
while (str[i] != '\0')//这里要用单引号
{
++true_length;//输入的length是数组最大长度,真实长度有待检测
if (str[i] == ' ')
++count;
++i;
} int new_length = true_length + * count;//算法核心思想,从尾到头复制
if (new_length > length)//要判断原数组长度够长不
return false;//难受,写的时候落下个分号 for (int i = count; i > ;)//当所有空格被替代完后结束
{
if (str[true_length] == ' ')
{
new_length=new_length - ;//如果该复制空格了,先提前移两位位,并赋值“%20”,注意是两位!!
str[new_length] = '%';//这里也要用单引号
str[new_length+] = '';
str[new_length+] = '';//这里也写错过,是+2,不小心写成+1!!
i--;//这个判断条件也写错过,刚才写进for()中了!!
}
else
str[new_length] = str[true_length]; new_length -= ;
true_length -= ; }
return true;
} // ====================测试代码====================
void Test(const char* testName, char str[], int length, const char expected[])
{
if (testName != NULL)
printf("%s begins: ", testName); replace_space(str, length); if (expected == NULL && str == NULL)
printf("passed.\n");
else if (expected == NULL && str != NULL)
printf("failed.\n");
else if (strcmp(str, expected) == )
printf("passed.\n");
else
printf("failed.\n");
} // 空格在句子中间
void Test1()
{
const int length = ; char str[length] = "hello world";
Test("Test1", str, length, "hello%20world");
} // 空格在句子开头
void Test2()
{
const int length = ; char str[length] = " helloworld";
Test("Test2", str, length, "%20helloworld");
} // 空格在句子末尾
void Test3()
{
const int length = ; char str[length] = "helloworld ";
Test("Test3", str, length, "helloworld%20");
} // 连续有两个空格
void Test4()
{
const int length = ; char str[length] = "hello world";
Test("Test4", str, length, "hello%20%20world");
} // 传入nullptr
void Test5()
{
Test("Test5", nullptr, , nullptr);
} // 传入内容为空的字符串
void Test6()
{
const int length = ; char str[length] = "";
Test("Test6", str, length, "");
} //传入内容为一个空格的字符串
void Test7()
{
const int length = ; char str[length] = " ";
Test("Test7", str, length, "%20");
} // 传入的字符串没有空格
void Test8()
{
const int length = ; char str[length] = "helloworld";
Test("Test8", str, length, "helloworld");
} // 传入的字符串全是空格
void Test9()
{
const int length = ; char str[length] = " ";
Test("Test9", str, length, "%20%20%20");
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();//是无效输入
Test6();//是空输入,这俩还不太一样
Test7();
Test8();
Test9(); system("pause");
}

《剑指offer》第五题(替换空格)的更多相关文章

  1. 《剑指Offer》算法题——替换空格

    题目:请实现一个函数,将一个字符串中的空格替换成“ % 20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are % 20Happy. class Solution ...

  2. 剑指Offer - 九度1510 - 替换空格

    剑指Offer - 九度1510 - 替换空格2013-11-29 20:53 题目描述: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之 ...

  3. 剑指offer面试题4 替换空格(java)

    注:利用java中stringBuilder,append,length方法很方便的解决字符串问题 /* * 剑指offer 替换空格 * xsf * */ /*开始替换空格的函数,length为原数 ...

  4. 剑指offer面试题4 替换空格(c)

  5. 剑指offer第二版-5.替换空格

    面试题5:替换空格 题目要求: 实现一个函数,把字符串中的每个空格都替换成“%20”,已知原位置后面有足够的空余位置,要求改替换过程发生在原来的位置上. 思路: 首先遍历字符串求出串中空格的数量,求出 ...

  6. 剑指Offer:面试题4——替换空格(java实现)

    问题描述:请实现一个函数,把字符串中的每个空格替换成"%20". 例如: 输入:"We are happy." 输出:"We%20are%20happ ...

  7. C++版 - 剑指offer 面试题4: 替换空格 题解

    面试题4:替换空格 提交网址: http://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=1 ...

  8. 剑指offer(2)替换空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 题目分析 我们如果要替换空格,两步 ...

  9. 剑指offer【02】- 替换空格(Java)

    题目:替换空格 考点:字符串 题目描述: 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. ...

  10. 剑指Offer(4)——替换空格

    题目: 请实现一个函数,把字符串中的每个空格替换成"%20".例如输入“We are happy.”,则输出“We%20are%20happy.”. 思路: 如果按照顺序从前往后依 ...

随机推荐

  1. Linux系统——JumpServer跳板机的搭建和部署

    公网源部署jumpserver跳板机 建立阿里云公网源yum仓库(服务端)[root@localhost ~]# lsanaconda-ks.cfg install.log.syslog jumpse ...

  2. 剑指offer4

    中序遍历(LDR)是二叉树遍历的一种,也叫做中根遍历.中序周游.在二叉树中,先左后根再右.巧记:左根右. 现在有一个问题,已知二叉树的前序遍历和中序遍历:PreOrder:         GDAFE ...

  3. c语言的字符串拷贝函数的精简

    #include <stdio.h>#include <string.h>void str_cpy(char * to, char *from){    while ((*to ...

  4. windows分区

  5. org.apache.catalina.core.StandardWrapperValve invoke的解决办法

    org.apache.catalina.core.StandardWrapperValve invoke的解决办法 比较容易错的地方是页面带参数进行跳转,由于跳转之后的页面本身也要执行一部分sql语句 ...

  6. mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)字符串拼接cancat实战例子

    mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)mysql字符串拼接cancat实战例子: mysql update set 多个字段相加,如果是数字相加可以直接用+号(注:hund ...

  7. 检测u盘是否挂载上方法

    打开内核log:echo "8" > /proc/sys/kernel/printk 关闭内核log:echo "1" > /proc/sys/ke ...

  8. web前端----css补充

    css常用的一些属性: 1.去掉下划线 :text-decoration:none ;2.加上下划线: text-decoration: underline; 3.调整文本和图片的位置(也就是设置元素 ...

  9. ACM题目————A Knight's Journey

    Description BackgroundThe knight is getting bored of seeing the same black and white squares again a ...

  10. ELK学习笔记之ElasticSearch的索引详解

    0x00 ElasticSearch的索引和MySQL的索引方式对比 Elasticsearch是通过Lucene的倒排索引技术实现比关系型数据库更快的过滤.特别是它对多条件的过滤支持非常好,比如年龄 ...