当前位置:网站首页>String replace space
String replace space
2022-07-03 01:59:00 【A little sigh】
subject : One string space is enough , Replace each space in the string with "%20", Such as "He ll o" Replace with "He%20ll%20o"
Ideas : First traverse the string , Get the number of spaces , To get the length of the new string . Traverse the string from the end of the string , Copy characters one by one , Replace with... When a space is encountered %20.
#include <iostream>
void trimBlank(char arr[], int length)
{
if (arr == NULL || length <= 0) {
return;
}
int index = 0;
int blankNumber = 0;
while (arr[index]!= '\0') {
if (arr[index] == ' ') {
blankNumber++;
}
index++;
}
int replaceIndex = length + blankNumber * 2;
int i = length;
while (i >=0 && replaceIndex > i) {
if (arr[i] == ' ') {
arr[replaceIndex--] = '0';
arr[replaceIndex--] = '2';
arr[replaceIndex--] = '%';
} else {
arr[replaceIndex--] = arr[i];
}
i--;
}
}
int main(int argc, const char * argv[]) {
char a[20] = {'h', ' ', 'e','l','l', ' ', 'o'};
trimBlank(a, 7);
std::cout<<a<<std::endl;
return 0;
}
边栏推荐
- 可視化yolov5格式數據集(labelme json文件)
- [AUTOSAR cantp] -2.11-uds diagnostic response frame data segment data padding data filling and data optimization data optimization (Theory + configuration)
- STM32 - switch of relay control lamp
- Analyzing several common string library functions in C language
- Network security - password cracking
- stm32F407-------DMA
- Network security - virus
- Custom components, using NPM packages, global data sharing, subcontracting
- STM32 - GPIO input / output mode
- [leetcode] 797 and 1189 (basis of graph theory)
猜你喜欢
Vant implements a simple login registration module and a personal user center
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
STM32 - switch of relay control lamp
[error record] navigator operation requested with a context that does not include a naviga
Depth (penetration) selector:: v-deep/deep/ and > > >
技术大佬准备就绪,话题C位由你决定
自定义组件、使用npm包、全局数据共享、分包
小程序開發的部分功能
Performance test | script template sorting, tool sorting and result analysis
Some functions of applet development
随机推荐
DDL basic operation
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
Network security OpenVAS
查询商品案例-页面渲染数据
Visual yolov5 format data set (labelme JSON file)
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
Redis:Redis的简单使用
技术大佬准备就绪,话题C位由你决定
es6 filter() 数组过滤方法总结
Ni visa fails after LabVIEW installs the third-party visa software
Technology sharing | Frida's powerful ability to realize hook functions
【Camera专题】手把手撸一份驱动 到 点亮Camera
微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
Network security - virus
Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
Rockchip3399 start auto load driver
LabVIEW安装第三方VISA软件后NI VISA失效