当前位置:网站首页>[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 .
边栏推荐
- The metamask method is used to obtain account information
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of power management units in the global market in 2022
- 7. Build native development environment
- Research Report on micro gripper industry - market status analysis and development prospect prediction
- Market trend report, technical dynamic innovation and market forecast of China's low gloss instrument
- JDBC | Chapter 3: SQL precompile and anti injection crud operation
- Adding data to the head or tail of the rar file can still decompress normally
- [12] the water of the waves is clear, which can wash my tassel. The water of the waves is muddy, which can wash my feet
- Golang string segmentation
- Who do you want to open a stock account? Is it safe to open a mobile account?
猜你喜欢

7. Build native development environment

rwctf2022_ QLaaS

Activation function - relu vs sigmoid
![[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)](/img/04/4070d51ce8b7718db609ef2fc8bcd7.jpg)
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)

Roommate, a king of time, I took care of the C language structure memory alignment

Write the content into the picture with type or echo and view it with WinHex

Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class

Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve

How does esrally perform simple custom performance tests?

ROS learning (10): ROS records multiple topic scripts
随机推荐
[12] the water of the waves is clear, which can wash my tassel. The water of the waves is muddy, which can wash my feet
Research Report on the overall scale, major manufacturers, major regions, products and applications of metal oxide arresters in the global market in 2022
Construction and maintenance of business websites [8]
This team with billions of data access and open source dreams is waiting for you to join
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
MySQL learning notes (Advanced)
Check the confession items of 6 yyds
Activation function - relu vs sigmoid
JS modularization
Accounting regulations and professional ethics [19]
[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of sound quality head simulators in the global market in 2022
Hot backup routing protocol (HSRP)
[dynamic planning] p1220: interval DP: turn off the street lights
[fluent] dart function (function composition | private function | anonymous function | function summary)
Accounting regulations and professional ethics [16]
Makefile: usage of control functions (error, warning, info)
I drew a Gu ailing with characters!
Research Report on the overall scale, major manufacturers, major regions, products and applications of capacitive voltage transformers in the global market in 2022
Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!