【字符串与数组】

Q:Write a method to replace all spaces in a string with ‘%20’

题目:写一个算法将一个字符串中的空格替换成%20

解答:


很直观的解法,首先统计出字符串中空格个数,然后分配新的内存空间,依次从头到尾复制原字符串到新字符串中,遇到空格,则复制%20这三个字符。还有没有其他更好点的方法呢??

char* replace(char* str){
int len=strlen(str);
int spaceNum=0;
int i;
for(i=0;i<len;++i){
if(str[i]==' ')
++spaceNum;
}
char* newStr=malloc((len+2*spaceNum+1)*sizeof(char));
int index=0;
for(i=0;i<len;++i){
if(str[i]==' '){
newStr[index++]='%';
newStr[index++]='2';
newStr[index++]='0';
}
else
newStr[index++]=str[i];
}
newStr[index]='\0';
return newStr;
}


作者:Viidiot  微信公众号:linux-code

[google面试CTCI] 1-5.替换字符串中特定字符的更多相关文章

  1. Java中替换字符串中特定字符,replaceAll,replace,replaceFirst的区别

    使用“;”替换过字符串中的“,” public class Test01 {public static void main(String[] args) {String number = " ...

  2. JS正则表达式获取字符串中特定字符

    JS正则表达式获取字符串中得特定字符,通过replace的回调函数获取. 实现的效果:在字符串中abcdefgname='test'sddfhskshjsfsjdfps中获取name的值test  实 ...

  3. C# 字符串中特定字符判断

    /// <summary> /// 计算字符串中子串出现的次数 /// </summary> /// <param name="str">字符串 ...

  4. [google面试CTCI] 1-8.判断子字符串

    [字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...

  5. [google面试CTCI] 2-1.移除链表中重复元素

    [链表] Q:Write code to remove duplicates from an unsorted linked list      FOLLOW UP      How would yo ...

  6. [SQL]replace替换字符串中的字符

    ','**') --下面是结果集 /* ----------- 12345678** */ SELECT replace(CONVERT(varchar(),GETDATE(),),'-','') - ...

  7. SQL 中 replace 替换字符串中的字符 ''

    update CfmRcd set reconsource=replace(reconsource,'''',''), cmffile =replace(cmffile,'''',''), cfmda ...

  8. 《Python CookBook2》 第一章 文本 - 替换字符串中的子串

    替换字符串中的子串 任务: 给定一个字符串,通过查询一个字符串替换字典,将字符串中被标记的子字符串替换掉. 解决方案: >>> import string >>> ...

  9. python3 替换字符串中指定位置字符

    大家都知道字符串在python中是不可变数据类型,那么我们如何替换字符串中指定位置的字符呢? 字符串转换列表替换并转换解决: def replace_char(string,char,index): ...

随机推荐

  1. python_random随机

    在数据清洗,评估 ,抽验等等过程中,经常有这样的应用场景 : 需要在一个大的数据集合中随机出来样本,进行人工评估.为了保证足够随机,借助脚本来实现. 下面一个脚本  ,用于应对这种应用场景. 使用方法 ...

  2. java_tomcat_the_APR based Apache Tomcat 小喵咪死活启动报错_临时方案

    报错信息如下: 信息: The APR based Apache Tomcat Native library which allows optimal performance in productio ...

  3. Android 2.3.5源码 更新至android 4.4,能够下载,度娘网盘

    Android 4.4源代码下载(linux合并) ==============================切割线结束========================= 旧版本号的能够使用115, ...

  4. IOS开发计算文本尺寸

    在IOS开发中例如微博,QQ聊天界面中要显示大量的文字信息,这样需要计算出文字部分的尺寸,才能设计出合适的控件尺寸和位置.下面是IOS 7.0计算文本尺寸的方法.- (CGRect)boundingR ...

  5. vistual studio 2012 安装失败,提示Microsoft Vistual Studio 2012 Devenv找不到元素,等错误信息

    在安装vistual studio 2012过程中,出现安装失败,提示Microsoft Vistual Studio 2012 Devenv找不到元素,等错误信息 解决方法是更新相应的server补 ...

  6. SSRS (SQL Server Report Service) 在IE9, IE10下显示不全的解决办法

    原文:SSRS (SQL Server Report Service) 在IE9, IE10下显示不全的解决办法 在做项目的过程中遇到SSRS与IE9, IE10不兼容的情况,具体表现为报表页面在IE ...

  7. selenium + python 部署自动化测试环境

    选择selenium和python其实是怀有私心的:码两行python,熟悉熟悉.    selenium优点很多,我最看重的是支持多语言,足够简单,同时支持浏览器. 实际工作中,简单实用真的太重要了 ...

  8. idea中ajax中文乱码

    case:@RequestMapping中添加 produces= "text/plain;charset=UTF-8", @RequestMapping(method = Req ...

  9. [转]JavaScript Namespaces and Modules

    Namespaces In most programming languages we know the concept of namespaces (or packages).Namespaces ...

  10. JavaScript实例技巧精选(12)—计算星座与属相

    >>点击这里下载完整html源码<< 这是截图: 核心代码如下: <SCRIPT LANGUAGE="JavaScript"> <!-- ...