当前位置:网站首页>[C language] [sword finger offer article] - replace spaces
[C language] [sword finger offer article] - replace spaces
2022-07-02 21:24:00 【:-D!! yzq】
Title Description
Please implement a function , Put the string s Replace each space in with "%20".
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
ideas
Using the double pointer idea , The first pointer traverses the array to find each space , And replace the space with ’\0’ , The second pointer points to the first address of the source string , And then use it strcat Move the address indicated by the second pointer to the first ’\0’ The string of is appended to the destination address , Last , The second pointer moves to the next address of the first pointer , loop , Until the source string is traversed .( The idea was inspired by the solution of a big man : The most coquettish I'm here , are you ready )
Code implementation
char* replaceSpace(char* s){
char *restr = NULL;
char *str = "%20";
int i = 0;
char *ptr = s;
int len = 0;
len = strlen(s);
restr = malloc(sizeof(char)*len*3+1);
if(restr == NULL)
{
printf("malloc failed!\n");
return NULL;
}
restr[0] = '\0';
for(i = 0; s[i] != '\0'; i++)
{
if(s[i] != ' ')
{
continue;
}
s[i] = '\0';
strcat(restr,ptr);
strcat(restr,str);
ptr = s + i + 1;
}
strcat(restr,ptr);
return restr;
}
When malloc And strcat When used in combination , One thing to note :malloc The allocated memory will be automatically added to the end address \0
,strcat Will appear first from the address \0
Start adding , So the sentence restr[0] = '\0';
Is the essence of the whole algorithm . At that time, there were always cross-border operations , Also depressed for a long time .
边栏推荐
- How does esrally perform simple custom performance tests?
- Analysis of enterprise financial statements [1]
- JDBC | Chapter 3: SQL precompile and anti injection crud operation
- Share the easy-to-use fastadmin open source system - Installation
- Add two numbers of leetcode
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of the inverted front fork of the global market in 2022
- [fluent] dart function (function composition | private function | anonymous function | function summary)
- Research Report on market supply and demand and strategy of Chinese garden equipment industry
- ROS learning (10): ROS records multiple topic scripts
- Number of DP schemes
猜你喜欢
Web3js method to obtain account information and balance
26 FPS video super-resolution model DAP! Output 720p Video Online
[fluent] dart technique (independent main function entry | nullable type determination | default value setting)
Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve
Hot backup routing protocol (HSRP)
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
An analysis of the past and present life of the meta universe
Interested parties add me for private chat
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
kernel tty_ struct
随机推荐
MySQL learning notes (Advanced)
Research Report on micro vacuum pump industry - market status analysis and development prospect prediction
A river of spring water flows eastward
When Valentine's Day falls on Monday
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
AES encryption CBC mode pkcs7padding filling Base64 encoding key 32byte iv16byte
Don't you want to have a face-to-face communication with cloud native and open source experts? (including benefits
qwb2018_ core kernel_ rop
kernel_ uaf
ROS learning (10): ROS records multiple topic scripts
China microporous membrane filtration market trend report, technological innovation and market forecast
Accounting regulations and professional ethics [19]
[fluent] dart function (function composition | private function | anonymous function | function summary)
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
Cloud computing technology [2]
在券商账户上买基金安全吗?哪里可以买基金
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of precoated metallic coatings in the global market in 2022
Construction and maintenance of business websites [6]
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of power management units in the global market in 2022
China's log saw blade market trend report, technological innovation and market forecast