当前位置:网站首页>Simulate the implementation of strstr
Simulate the implementation of strstr
2022-07-25 02:07:00 【VisionaryHS】
strstr The arguments to the function are two char* Pointer to type , The return value is char* Pointer to type , Its function is to find whether the substring in the string exists , If there is, then return str1 Neutron string ( originate str2) From , If it does not exist , Return null pointer
Let's implement mystrstr
- First of all, design the parameter and the original function strstr equally
- Assert first after entering the function p1 and p2 Null pointer or not
- keep p2 Immobility , if p1 and p2 The content pointed to is different , be p1++
- When p1 Content and p2 At the same time , use char*start Record p1 The address of
- Redefine flag, Used to judge whether to enter while loop , Definition count Used to record p1 After entering the second cycle, compared with start Offset of position .
- Under normal circumstances ,*p1==*p2,p1 and p2 own ++,count also ++
- If you encounter the end of the substring you need to find , That is to say *p2==‘\0’ when flag=0. End of cycle , And back to start The address of
- You may enter else sentence . The reason is that there are duplicate characters . Now start It is necessary to record the starting position again


char* mystrstr(const char* p1, const char* p2)
{
assert(p1 && p2);
while (*p1 != *p2)
{
p1++;
}
char* start = p1;
int count = 0;
int flag = 1;
while (flag)
{
if (*p1 == *p2 )
{
p1++;
p2++;
count++;
}
else if (*p2 == '\0')
{
flag=0;
}
else
{
p2--;
start++;
}
}
return start;
}char* mystrstr(const char* p1, const char* p2)
{
assert(p1 && p2);
while (*p1 != *p2)
{
p1++;
}
char* start = p1;
int count = 0;
int flag = 1;
while (flag)
{
if (*p1 == *p2 )
{
p1++;
p2++;
count++;
}
else if (*p2 == '\0')
{
flag=0;
}
else
{
p2--;
start++;
}
}Running results

边栏推荐
- iptables :chains, target
- [summer daily question] Luogu p1706 full ranking question
- "I gave up programming and wrote a 1.3 million word hard science fiction."
- Promise basic use
- Standard transfer function
- Focus on improving women's and children's sense of gain, happiness and security! In the next ten years, Guangzhou Women's and children's undertakings will make such efforts
- Gerrit statistics script
- Kernel structure and design
- Thinkphp5.0.24 deserialization chain analysis
- Vs2019 configuring Qt5 development environment
猜你喜欢

How to judge which star you look like?

Mobile Robotics (3) Kalman filter

See you in Suzhou! "Cloud Intelligence Technology Forum - industry special" will be held soon

Ireport export PDF font bold failure

Summary of the most complete MySQL data types in history (Part 2)

Opengauss kernel analysis: query rewriting

Take C language from 0 to 1 - program structure and use examples

How to obtain workers' coats and helmets in stray? How to obtain workers' helmets

Inventory of well-known source code mall systems at home and abroad

Peripherals: interrupt system of keys and CPU
随机推荐
Management mode of agricultural science data center based on life cycle theory
Peripherals: timer, watchdog and RTC
Cloud native platform, let edge applications play out!
Create the first hbuilder project
C traps and defects Chapter 2 lexical "traps" 2.4 switch statements
Data governance notes
POM reports an error
R language uses logistic regression, ANOVA, outlier analysis and visual classification iris iris data set
Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
Interpretation of video generation paper of fed shot video to video (neurips 2019)
Industrial control safety PLC firmware reverse II
Take the first place in the International Olympic Games in mathematics, physics and chemistry, and win all the gold medals. Netizen: the Chinese team is too good
Deamnet|filenotfounderror: [winerror 3] the system cannot find the specified path.: '/ Datasettest\\Set12‘
About the relationship between parent process and child process (UAC bypass idea)
Nacos service discovery data model
July 8, 2022
Contemporary fairy quotations
C#/VB. Net insert watermark in word
"Nowadays, more than 99.9% of the code is garbage!"
Query the thread information of MySQL server (detailed explanation)