#include <stdio.h>

 void ReplaceBlank(char string[],int length){
if(string == NULL || length == 0){
return;
} int originalLength = 0;//
int blankCount = 0; while(string[originalLength] != '\0'){
if(string[originalLength] == ' '){
++blankCount;
}
++originalLength;
} int newLength = originalLength + blankCount * 2; if(newLength > length){//not enough space
return;
} int indexFirst = originalLength;
int indexSecond = newLength;
while(indexFirst>=0 && indexSecond>indexFirst){
if(string[indexFirst] == ' '){
string[indexSecond--] = '0';
string[indexSecond--] = '2';
string[indexSecond--] = '%';
}else{
string[indexSecond--] = string[indexFirst];
}
--indexFirst;
}
} int main(){
char str[50] = "We are happy.";
printf("str = %s\n",str); ReplaceBlank(str,50);
printf("str = %s\n\n",str); char str1[50] = "Wearehappy.";
printf("str1 = %s\n",str1); ReplaceBlank(str1,50);
printf("str1 = %s\n\n",str1); char str2[50] = " We are happy.";
printf("str2 = %s\n",str2); ReplaceBlank(str2,50);
printf("str2 = %s\n\n",str2); char str3[50] = "";
printf("str3 = %s\n",str3); ReplaceBlank(str3,50);
printf("str3 = %s\n\n",str3); return 0;
}

结果

str = We are happy.
str = We%20are%20happy. str1 = Wearehappy.
str1 = Wearehappy. str2 = We are happy.
str2 = %%%20We%20are%20happy. str3 =
str3 =

剑指offer 04_替换字符串中的空格的更多相关文章

  1. 【剑指offer】替换字符串中的空格

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/25002199 剑指offer上的第四道题目,在九度OJ上測试通过,但还是有些问题.由于是用 ...

  2. 【剑指offer】将字符串中的空格替换成"%20"

    #include <iostream> #include <string> using namespace std; char *ReplaceSpace(char *str, ...

  3. 剑指Offer 找出字符串中第一个只出现一次的字符

    题目描述 找出字符串中第一个只出现一次的字符 如果无此字符 请输出'.' 输入描述: 输入一串字符,由小写字母组成 输出描述: 输出一个字符 输入例子: asdfasdfo 输出例子: o 思路:数组 ...

  4. 《剑指offer》-找到字符串中第一个只出现一个的字符

    题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...

  5. 代码随想录第八天 |344.反转字符串 、541. 反转字符串II、剑指Offer 05.替换空格 、151.翻转字符串里的单词 、剑指Offer58-II.左旋转字符串

    第一题344.反转字符串 编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 s 的形式给出. 不要给另外的数组分配额外的空间,你必须原地修改输入数组.使用 O(1) 的额外空间解决这 ...

  6. 《剑指offer》替换空格

    本题来自<剑指offer> 替换空格 题目: 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are% ...

  7. 剑指 Offer 44. 数字序列中某一位的数字 + 找规律 + 数位

    剑指 Offer 44. 数字序列中某一位的数字 Offer_44 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author Wale ...

  8. 菜鸟刷题路:剑指 Offer 05. 替换空格

    剑指 Offer 05. 替换空格 class Solution { public String replaceSpace(String s) { StringBuilder str = new St ...

  9. 力扣 - 剑指 Offer 67. 把字符串转换成整数

    题目 剑指 Offer 67. 把字符串转换成整数 思路1 根据题意,要解决这题,首先要判断的条件有: 不包括首位空格 第一位必须为:+.-.数字三者其一,否则不合法 数字必须连续的,如果遇到非数字, ...

随机推荐

  1. javascript 跨域问题解决办法总结

    跨域的意思就是不同域名之间的页面默认是无法通信的.因为浏览器默认是禁止跨域的: 图所示:chrome浏览器尝试获取mainFrame失败,提示DomException 1).假如你有个网站 a.com ...

  2. android之Uri的常用几个例子

    显示网页:   1. Uri uri = Uri.parse("http://www.google.com");   2. Intent it = new Intent(Inten ...

  3. FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask

    hive启动后,出现以下异常 hive> show databases; FAILED: Error / failed on connection exception: java.net.Con ...

  4. file_put_contents();

    file_put_contents(); 用于获取文件中的内容,可以填写网址,但是需要以http://开头

  5. centos 下配置oracle11gR2开机自启

    这里使用的环境是 CentOS 6.6 ,并且已经装好了oracle11gR2 oracle启动分为两个步骤: 1.启动监听 2.启动服务 1.root 用户下修改ORATAB(将N该为Y): [ro ...

  6. Yii错误404页面

    'errorHandler'=>array( // use 'site/error' action to display errors 'errorAction'=>YII_DEBUG ? ...

  7. [转]html5 video在安卓大部分浏览器包括微信最顶层的问题

    能否解决video标签置顶的问题?答案是:不行的. 具体可查看官方解释:http://x5.tencent.com/guide?id=2009 //====================201604 ...

  8. git之切换分支出现的问题

    当在其他分支,如test分支开发的时候,新增了文件夹等目录结构.开发完成后,切换会master分支. 如果出现“Deletion of directory '***' failed. Should I ...

  9. 开启 intel vt-d

    1.开机后按“DEL”或“F2”进入BIOS: 2.在Advanced选项页中找到System Agent Configuration并选择进入: 3.进入System Agent Configura ...

  10. bae3.0第四步 第一个polls系统

    1.创建自己的app 进入新建的blog工程目录,执行其下面的manage.py来创建polls应用,命令为: python manage.py startapp polls2.定义app的model ...