当前位置:网站首页>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;
}
边栏推荐
- Return the only different value (de duplication)
- Modify table structure
- 浏览器是如何对页面进行渲染的呢?
- Network security - the simplest virus
- Caused by: com. fasterxml. jackson. databind. exc.MismatchedInputException: Cannot construct instance o
- [fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
- 2022 spring "golden three silver four" job hopping prerequisites: Software Test interview questions (with answers)
- 【Camera专题】OTP数据如何保存在自定义节点中
- 网络安全-浅谈安全威胁
- NCTF 2018 part Title WP (1)
猜你喜欢
stm32F407-------ADC
Processing of tree structure data
Rockchip3399 start auto load driver
MySQL learning 03
"Jetpack - livedata parsing"
网络安全-漏洞与木马
微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
Depth (penetration) selector:: v-deep/deep/ and > > >
Query product cases - page rendering data
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
随机推荐
疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
Network security NAT network address translation
His experience in choosing a startup company or a big Internet company may give you some inspiration
Certaines fonctionnalités du développement d'applets
Network security - cracking system passwords
Query product cases - page rendering data
STM32 - switch of relay control lamp
Network security - phishing
Ni visa fails after LabVIEW installs the third-party visa software
Problems encountered in small program development of dark horse shopping mall
可視化yolov5格式數據集(labelme json文件)
Redis:Redis的简单使用
Where is the future of test engineers? Confused to see
【Camera专题】Camera dtsi 完全解析
A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
stm32F407-------DMA
[leetcode] 797 and 1189 (basis of graph theory)
微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
¢ growth path and experience sharing of getting an offer