Ch1-5: Write a method to replace all spaces in a string with ‘%20’.

避坑指南:Unity新版InputSystem在移动端的5个常见问题及解决方案 本文针对Unity新版InputSystem在移动端开发中的常见问题,提供了5个关键解决方案,包括虚拟摇杆灵敏度调节、多点触控冲突处理、按钮反馈延迟优化、手势识别与陀螺仪数据融合以及Android/iOS平台差异处理。这些实战经验帮助开发者高效解决触控适配难题,提升移动游戏体验。 阅读详情

在剑指offer里面有提到这道题:Write a method to replace all spaces in a string with ‘%20’. 原因是server不能识别空格或者一些字符,所以将data send 给server前要将空格换成一些能识别字符(就像正则一样,有时候为了以后的模式识别,故意增加一些换行,---,%20之类的features,作为flag方便以后提取)。

#include <iostream>
#include <cstring>
using namespace std;

char* replace1(char* c){
    if(c == NULL) return NULL;
    int len = strlen(c);
    if (len == 0 ) return NULL;
    int cnt;
    for (int i=0; i< len; ++i){
        if (c[i]==' '){
            ++cnt; 
        }
    }
    
    char* cc = new char[len + 2*cnt+1];
    int p=0;
    for (int i=0; i < len; ++i){
        if(c[i]==' '){
            cc[p] = '%';
            cc[p+1]='2';
            cc[p+2]='0';
            
            p += 3;
        }    
        else{
            cc[p++]=c[i];   
        }
    }
    cc[p] = '\0';
    return cc;
}

int main()
{
   char hw[] = "Hello   World  !";
   cout << hw << endl; 
   int len = strlen(hw); 
   cout << len << endl;
   //char hw1[] = "";
   //hw1 = replace1(hw);
   cout << replace1(hw)<< endl;
   return 0;
}

还有另一个解法是当原来字符串足够大的时候,应该从后往前修改而不要新开一个cc[]空间。但是为什么要从后往前呢?

#include 
#include 
using namespace std;

char* replace1(char* c){
    if(c == NULL) return NULL;
    int len = strlen(c);
    if (len == 0 ) return NULL;
    int cnt;
    for (int i=0; i< len; ++i){
        if (c[i]==' '){
            ++cnt; 
        }
    }
    
    char* cc = new char[len + 2*cnt+1];
    int p=0;
    for (int i=0; i < len; ++i){
        if(c[i]==' '){
            cc[p] = '%';
            cc[p+1]='2';
            cc[p+2]='0';
            
            p += 3;
        }    
        else{
            cc[p++]=c[i];   
        }
    }
    cc[p] = '\0';
    return cc;
}

void replace2(char* c){
    if(c==NULL) return;
    int len=strlen(c);
    if(len==0) return;
    int cnt=0;  // if not declare to 0, will cause
                // segmentation fault 
    for (int i=0;i=0; --i){
        if (c[i]==' '){
            c[p]='0';
            c[p-1]='2';
            c[p-2]='%';
            p -=3;
        }
        else{
            c[p]=c[i];
            --p;
        }
    }
}

int main()
{
   char hw[50] = "Hello   World  !";
   cout << hw << endl; 
   int len = strlen(hw); 
   cout << len << endl;
   //char hw1[] = "";
   //hw1 = replace1(hw);
   cout << replace1(hw)<< endl;
   char hw2[50] = "WT  F !";
   
   cout << hw << endl;
   replace2(hw);
   cout << hw << endl;
   return 0;
}
Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1


Executing the program....
$demo 
Hello   World  !
16
Hello%20%20%20World%20%20!
Hello   World  !
Hello%20%20%20World%20%20!


12S BMS 核心设计:基于 STM32H743+BQ34Z100 的 SOC/SOH 实现与全链路技术解析(上) 本文详细介绍了基于STM32H743和BQ34Z100的12串锂电池管理系统(BMS)设计。该系统采用主控制器+专用电量监测计架构,可实现高精度SOC(±1%)和SOH(±2%)估算,支持36V-50.4V电压范围。硬件设计包括电压/电流采样、温度测量、保护电路等模块,软件实现基于HAL库开发,采用DMA方式提高I2C通信效率。系统通过TI专利的ImpedanceTrack™算法实现三维融合估算,并具备完善的保护功能。该设计适用于电动工具、轻型电动车等中功率设备,具有精度高、响应快。 阅读详情

相关推荐

[XJTUSE DATABASE]——第三章 关系数据库标准语言SQL

新年了,来个大的,写了好几天,将近9000字,希望对大家有所帮助

weixin_47692652的博客 3123

【待】1.4 Write a method to replace all spaces in a string with'%20'.

Write a method to replace all spaces in a string with'%20'. You may assume thatthe string has sufficient space at the end of the string to hold the dditionalcharacters, and that you are given the "tr...

weixin_30411997的博客 146

工业缺陷检测深度学习方法综述

异常:点异常、 上下文异常和集群异常。点异常:又称为离群值 (outliers)[9], 描述数值上偏离正 常样本的独立数据。与异常相近的概念还包括新颖点(novelty)和分布外数据。上下文异常:同样描述数据点,其数值属于正常范围,但不符合局部上下文规律。集群异常:描述一系列相关数据的集合,集合中的每一个实例的数值在单独考察时都处于正常值域,但集 合整体的相关性特征不服从正常模式。一般的图像异常检测往往仅需区分正常与异常样本,而工业缺陷检测更关注于检测 图像中的异常像素.

2301_80355452的博客 1845

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

Write a method to replace all spaces in a string with ‘%20’. /** * InterTest * cn.ustc.inter.array_string * ReplaceSpace.java * @version 1.0.0 * 2012-4-11 下午1:52:36 * Copyright (c) ustcxjt

ustcxjt的专栏 1808

temu登录接口逆向分析(含Py纯算)

【代码】temu登录接口逆向分析(含Py纯算)

数据精灵喵 1949

无法启动此程序,因为计算机丢失api-ms-win-core-path-l1-1-0.dll的解决方案

本文主要介绍了无法启动此程序,因为计算机丢失api-ms-win-core-path-l1-1-0.dll的解决方案,希望能对使用Python的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 20万+

#212 Space Replacement

题目描述: Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true lengt

易水居 415

第 8 篇:TMC2240 电机正反转实现|DIR 引脚控制 + 代码优化(稳定不抖动)

本文详细介绍了TMC2240电机驱动芯片的正反转实现方法,重点解决方向切换时的抖动和冲击问题。文章首先明确了硬件连接、基础配置和使能状态三个前提条件,然后阐述了DIR引脚电平控制方向的核心原理,提供了STM32的GPIO配置代码。针对方向切换时的电流冲击问题,作者提出了"停止-延迟-切换-启动"的平滑切换逻辑,并给出了包含方向枚举、转速控制和方向切换函数的完整代码实现方案。通过这种优化方法,可以有效避免电机在正反转切换时产生的抖动、冲击声和发热问题,实现稳定的双向控制。文章还提示了后续可

BackCatK Chen的博客 2084

Q1.4 Write a method to replace all spaces in a string with ‘%20’.

思路: 1、遍历, 原来的字符串长度len, 记录空格个数cnt, 则最后的字符串长度len+2*cnt。 2、从后往前遍历,遇到空格将其替换为"%20",否则原样拷贝 #include #include using namespace std; void replace(char *c){ if(c == NULL) return; int len = strlen(

xinxinxv的专栏 578

手把手教你学Simulink——基于Simulink的固态变压器(SST)多级协同控制

摘要:本文详细介绍了基于Simulink的固态变压器(SST)多级协同控制方法。SST作为新一代智能电网核心设备,通过AC-DC整流、DC-DC隔离和DC-AC逆变三级架构实现电能高效转换。针对多级耦合难题,提出能量-功率协同控制策略:纵向解耦电压环与功率前馈,横向联动级间功率指令。在Simulink中搭建10kV/400V系统模型,实现直流母线电压波动<2%的动态性能。特别设计了负载突变时的三级协同机制,通过虚拟惯性算法和前馈控制实现快速响应。文章包含完整的建模步骤、控制推导和参数设置,为电力电子工

MHD0815的博客 198

CAN总线数据采集与分析

在CAN总线数据采集中,数据采集是指从CAN网络中获取数据,并将其存储在本地或远程数据库中。数据源:明确数据源,如CAN总线、CANoe/CANalyzer工具等。数据格式:确定数据格式,如CAN消息、信号值、环境变量等。数据存储:选择数据存储方式,如本地文件、数据库、云存储等。在CAN总线数据分析中,数据分析是指对采集到的数据进行处理和分析,以提取有用的信息。数据处理:对采集到的数据进行处理,如滤波、去噪、归一化等。数据分析:对处理后的数据进行分析,如统计分析、趋势分析、故障诊断等。数据可视化。

闲人编程的博客 4045

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

exclusively for XU-bp75mklu3ja@checkout.google.com 21.1 Write a method to replace all spaces in a string with ‘%20’. pg 60 SOLUTIONS The algorithm is as follows: 1. Count the number of spaces during the first scan of the string. 2. Parse the string ag

Jeffery的面试题库 1507

PCIe(二) —— 配置空间

好了,这一篇我们把配置空间的结构,分配和访问,都大概总结了一遍,并且还介绍了它们的头部字段和这些字段的意义,包括BAR和消息路由的原理。之后,如果有时间,我们会继续来探索PCIe的数据报文的结构和传输等等。

叫好与叫座虽然不是对立面,但想在同一个作品中达到双重效果很难。 1770

Play with sunbeam again (by quqi99)

之前玩过sunbeam, 都命令都过时了,Using sunbeam to deploy openstack - https://zhhuabj.blog.csdn.net/article/details/133840856。国内家中机器由于特色跟国外公司机器一样也都属于内网机器,所以上面的办法一样适用。并且这次是将sunbean部署在公司测试机器上,但全是内网环境,需要通过proXX才能访问外网。

技术并艺术着 504

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Leno 语言入门指南

《Leno语言入门指南》摘要 Leno是一门介于Python和Go之间的静态类型检查脚本语言,融合了动态语言的灵活性和静态类型的安全性。该指南全面介绍了Leno的核心特性: 基础语法:支持类型推断(var)、任意精度整数、并行赋值和代码块作用域 类型系统:包含int(自动处理溢出)、float、string、bool等基础类型,以及泛型数组和字典 控制流:提供if/switch条件语句、多种for循环形式以及类型守卫功能 函数编程:支持高阶函数、匿名函数、闭包和默认参数 面向对象:通过struct实现,包含

cheng8214的专栏 318

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

1.5-替换字符串中的空格为%20

Write a method to replace all spaces in a string with ‘%20’. 先计算空格数量,然后malloc新的z char* replaceSpaces(char* str) { char *p = str; int len=0,spaceNum=0; while(*p) { len++;

oooooops 538

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

cc150做题计划

计划:12.29.2013 - 1.29.2013 做完第一遍题目,然后2月做leetcode。 2月底面试前完成200题C++。 这里记录一下ctci学习的参考资料。 1. hawstein的blog 2. https://github.com/ChengTian/CtCI 3. absolute C++ 5th 4. Primer C++ 5. learncpp:http

纯黑老白的专栏 2909

ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods

In this problem, it is important to fully understand the definition of least common ancestor of 2 nodes: it means: (from leetcode blog: 点击打开链接) _______3______ / \

纯黑老白的专栏 1330

Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)

这个有点像DSP FPGA Chang Choo的题目,一般是放大插值,DFT,IIR之类的Verilog题目。 题目在这:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees.

纯黑老白的专栏 1295

Ch2-2: return the nth to the last node data of a singly linked list

In this problem, I need to find the nth to the last node data of a singly linked list.  In order to do so, one good way is to use recursion, since it is automatically put recursion part into stack an

纯黑老白的专栏 1290

1/22/2014: 纪念第一个C++ programmer电面--F家

经过20天的CC刷题(其实是学习),才刚刚做到第四章。看看其他非ACM牛人,都是leetcode,CC,EPI滚瓜烂熟才去面。而且onsite确实是靠实力,要有做过大project之后才能真正融会贯通C++精神。 Anyway,谢谢郭志给的refer,不然连电面的机会都没有。 开头是C++基础题: 1. Fib的2种写法:recursive和DP 2. Struct和Class的区别;在C

纯黑老白的专栏 1274

Ch1-7: clear the row i and column j iff a[i][j]==0

in this problem, both monish001 and hawstein use O(n^2) solution which first tranverse the char** a to find the a[i][j]==0 and mark that row/col to be: TRUE. Then the send loop make the TURE row/col

纯黑老白的专栏 1207

复习一下dfs和bfs 用java

dfs 和 bfs都是用来找从source 到 target的path的算法。 dfs用的是recursion,bfs则用queue

纯黑老白的专栏 1191

Ch3-1: use a single array to implement three stacks.

Here it explains why Hawstein use this in his constructor, because he use size a parameter in his constructor while "size" is also a member variable(it's private), so he use this->size = size, explici

纯黑老白的专栏 1097

ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one

// Ch4.7: You have two very large binary trees: T1, with millions of nodes, and T2, // with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 #include #include #includ

纯黑老白的专栏 1030

复习recursion

1. 如果这样 private void lop(int x, int size, double[] jerry){ if(x==size-1){ jerry[x] = -1; return; } if(jerry[x+1]==0){ jerry[x+1] = jerry[x]- 1; //if so, lop(x+1, size, jerry); }

纯黑老白的专栏 1009
上一篇: Ch1-4: anagram string
下一篇: Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值