当前位置:网站首页>C secret script Chapter 3 (detailed explanation of string function) (Section 2)
C secret script Chapter 3 (detailed explanation of string function) (Section 2)
2022-06-12 14:24:00 【Mortal programming biography】
In the last section, I sent it out because I didn't finish writing it , I'm sorry for you , In the last section, we introduced the usage and Simulation Implementation of two string functions , Next, we will continue to discuss .
Add : Last time strlen The return value of the function is size_t(unsigned int) Unsigned shaping , This is used by library functions typedef Keyword defines a type , So this function should pay attention to the pit .
The following code :
#include<stdio.h>
int main()
{
if(strlen("abc")-strlen("abcdef")>0)
{
printf("hehe\n");
}
else
{
printf("haha\n");
}
return 0;
}The result of the above code is :hehe, the reason being that strlen Function is an unsigned integer , Unsigned integer has no negative number , So even abc Do than abcdef Small subtraction or >0 Of .
3.strcat function
explain : This function has something similar to strcpy function , however strcpy The function copies a string to the element starting with the first character , and strcat The function appends a string to the destination string from \0 Starting position . Parameters 1: Destination string , Parameters 2: Original string
Ideas : Since it is from the destination string \0 Start appending the position of , Then we have to find the destination string first \0 The location of , Then start adding .
The implementation code is as follows :
#include<stdio.h> #include<assert.h> char* my_strcat(char* str1,const char* str2) { assert(str1!=NULL && str2!=NULL); // Determine the validity of the two pointers char* ret=str1; // Because the back str1 The element address of will change , Therefore, initializing the first element address is convenient for the following parameters to return and print . while(*str1 !='\0') // find \0 Location { str1++; // We can't put autoincrement here while In the condition of , Because we are looking for \0 Location , Before the last judgment, he will increase himself , After it comes out, it is not \0 Location but \0 Back address . } while(*str1++=*str2++) // hold str2 The element of is appended to str1 { ; } return ret; // return str1 Initial address . } int main() { char arr1[30]="hello"; // Note that the size of the array space must be given here , The default is just the size it is now storing , If you do not specify the size, you will not be able to fit it . char arr2[]="world"; my_strcat(arr1,arr2); printf("%s\n",arr1); return 0; }
4.strcpy function
function : This function is similar to the previous strcat function , It's a little simpler , Don't look for \0 Copy directly from the first element , Parameters 1: Destination string , Parameters 2: String to copy .
Ideas : Copy directly from the first element .
The implementation code is as follows :
#include<stdio.h> #include<assert.h> char* my_strcpy(char* str1,const char* str2) { assert(str1!=NULL && str2!=NULL) char* ret=str1; while(*str1++=*str2++;) { ; } return ret; } int main() { char arr1[15]="abc"; // Follow strcat Function to initialize the array size , Make sure you can fit two strings . char arr2[]="def"; my_strcpy(arr1,arr2); printf("%s\n",arr1); return 0; }
Key points :5.strstr function
explain : This function is a search substring function , Parameters 1: String to find , Parameters 2: Substring to find . If you find the return address , If it cannot be found, it returns a null pointer .
Ideas : This kind of function is more complicated , We should discuss it according to the situation , The first is arr1 Than arr2 Small, that means arr2 Absolutely not arr1 Substring of , In another case, their two strings are equal and both are \0 That means they arr2 yes arr1 Substring of , Note that the initial search address should be kept during the search process , For the next search .
The specific code is as follows :
#include<stdio.h> #include<assert.h> char* my_strstr(const char* str1,const char* str2) { assert(str1!=NULL && str2!=NULL); char* s1=NULL; char* s2=NULL; char* cor=(char*)str1; if(*str2=='\0') { return str1; // Each string has \0,str2 It must be str1 Substring of . } while(cor) // if cor(str1) Smaller than substring ,str2 It must not be str1 Substring of { s1=cor; s2=(char*)str2; while((*s1 != '\0' && *s2 != '\0') && (*s1==*s2)) // if s1 and s2 None of them is equal to \0 And equal { s1++; s2++; } if(*s2=='\0') { return cor; // If substring has reached \0 explain str2 yes str1 Substring of } else if(*s1=='\0') { return NULL; // if str1 Than str2 Arrive ahead of time \0 that str2 It must not be str1 Substring of } cor++; } return NULL; } int main() { char arr1[]="abbbcdef"; char arr2[]="bbc"; char* ret=my_strstr(arr1,arr2); if(ret==NULL) { printf(" String does not exist \n"); } else { printf("%s\n",ret); } return 0; }
OK, that's all for this section , If you have any mistakes, please tell me , I must accept , Take leave !
边栏推荐
猜你喜欢

Two methods of QT using threads

Redis核心配置和高级数据类型

TestEngine with ID ‘junit-vintage‘ failed to discover tests

Mold and remainder

【活动早知道】LiveVideoStack近期活动一览

Lua tvalue structure

SystemC common errors

Lua callinfo structure, stkid structure resolution

Mémoire de l'examen d'entrée à l'université

初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS
随机推荐
Alicloud development board vscode development environment setup
如果要打造品牌知名度,可以选择什么出价策略?
Shell脚本到底是什么高大上的技术吗?
Wait function in SystemC
通信流量分析
SystemC uses SC_ report_ Handler processing log printing
Socket model of punctual atom stm32f429 core board
工具笔记 —— 常用自定义工具类(正则,随机数等)
280 weeks /2171 Take out the least number of magic beans
Player actual combat 12 QT playing audio
Axi4 increase burst / wrap burst/ fix burst and narrow transfer
Why do Chinese programmers change jobs?
使用make方法创建slice切片的坑
What is automatic bidding? What are its advantages?
C secret script Chapter 1: data storage (in-depth analysis) supplement
【OCR】AspriseOCR C# 英文、數字識別(中文不行)
完美收官|详解 Go 分布式链路追踪实现原理
高考回忆录
Mémoire de l'examen d'entrée à l'université
Mold and remainder