当前位置:网站首页>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;
}
边栏推荐
- Huakaiyun (Zhiyin) | virtual host: what is a virtual host
- STM32 - introduction of external interrupts exti and NVIC
- Network security - scan
- 网络安全-防火墙
- 树形结构数据的处理
- [data mining] task 1: distance calculation
- Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
- Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
- 力扣(LeetCode)183. 从不订购的客户(2022.07.02)
- Function definition and call, this, strict mode, higher-order function, closure, recursion
猜你喜欢
Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
mysql
STM32 - GPIO input / output mode
stm32F407-------IIC通讯协议
网络安全-漏洞与木马
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
树形结构数据的处理
微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
小程序开发的部分功能
[data mining] task 1: distance calculation
随机推荐
网络安全-动态路由协议RIP
Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
Rockchip3399 start auto load driver
When the epidemic comes, how to manage the team as a leader| Community essay solicitation
Network security - virus
¢ growth path and experience sharing of getting an offer
MySQL learning 03
Ni visa fails after LabVIEW installs the third-party visa software
Where is the future of test engineers? Confused to see
LabVIEW安装第三方VISA软件后NI VISA失效
Analysis, use and extension of open source API gateway apisex
Why can't the start method be called repeatedly? But the run method can?
ByteDance data Lake integration practice based on Hudi
Storage basic operation
可视化yolov5格式数据集(labelme json文件)
小程序开发黑马购物商城中遇到的问题
[camera topic] how to save OTP data in user-defined nodes
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
[AUTOSAR cantp] -2.11-uds diagnostic response frame data segment data padding data filling and data optimization data optimization (Theory + configuration)