当前位置:网站首页>C language advanced part III. string functions and memory operation functions
C language advanced part III. string functions and memory operation functions
2022-07-24 07:32:00 【And move forward with the high wind - & gt;】
Catalog
Memory manipulation function :
String function :
strlen
size_t strlen ( const char * str)
{
const char *eos = str;
while( *eos++ ) ;
return( eos - str - 1 );
}
strcpy
char* strcpy(char* const destination,char const*source)
{
char* des = destination;
while ((*destination++ = *source++) != '\0') { }
return des;
}strcat
Prototype :char * strcat ( char * destination, const char * source );
String append function , Realization :
char * strcat (char * dst,const char * src)
{
char * cp = dst;
while( *cp )
cp++; /* find end of dst */
while((*cp++ = *src++) != '\0') ; /* Copy src to end of dst */
return( dst ); /* return dst */
}Find the destination string first ‘\0’ And then from ‘\0’ Start appending to the source string ‘\0’ Also copy the past , so The target space must be large enough , It can hold the contents of the source string . The target space must be modifiable . The source string must be in '\0' end .
strcmp
int strcmp ( const char * str1, const char * str2 );
Compare the characters of two strings in turn ASCII Code value , The return value is specified as follows :
The standard stipulates :
The first string is larger than the second string , Return greater than 0 The number of
The first string is equal to the second string , Then return to 0
int strcmp (const char * src, const char * dst)
{
int ret = 0 ;
while((ret = *(unsigned char *)src - *(unsigned char *)dst) == 0 && *dst)
{
++src, ++dst;
}
return ((-ret) < 0) - (ret < 0); // (if positive) - (if negative) generates
//branchless code
// Limit the return value to 0,1,-1
}strncpy
char * strncpy ( char * destination, const char * source, size_t num );
strncat
char * strncat ( char * destination, const char * source, size_t num );
strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
Similar to the above string function with limited length , But there is one more parameter num : The number of characters , You can specify the number of characters for the operation .
strstr
A function that finds a substring in a parent string , Will return the character pointer of the first substring found .
Function implementation :( The original method finds the substring , Than KMP,sunday And other algorithms are inefficient )
From a string str1 Find string in str2
char * strstr (const char * str1,const char * str2)
{
char *cp = (char *) str1;
char *s1, *s2;
if ( !*str2 ) Substring str2 It's empty , Return parent string
return((char *)str1);
while (*cp)
{
s1 = cp; String pointer
s2 = (char *) str2; Substring pointer
while ( *s2 && !(*s1-*s2) )
s1++, s2++;
if (!*s2) If the substring pointer points to ‘\0’, Find success , Return the corresponding parent string position
return(cp);
cp++;
}
return(NULL);
}
String cutting function
strtok
Prototype :![]()
For example, two strings :
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
Usage method
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );// Pass in NULL, The function will start from last '\0' Start cutting at the back
}
After each cutting with this function, this function will return the address before cutting , And modify the original string cutting character to '\0', The next cut will be from ‘\0’ Start , In this way, you will get
A
string
of
tokens
and
some
more
tokens
strerror

Pass in the error code errno, This function returns the first address of the error message string , Used to print error messages
![]()
Memory manipulation function :
memcpy
Memory copy function , Definition :

memmove
And memcpy similar , But it's more powerful , Can achieve destination and source Overlapping copies , Because function implementations are copied from overlapping areas .
Simulation Implementation

memcmp
Compare two memory areas byte by byte , similar strcmp The function compares the corresponding data ASCII Code value .
Realization :
int memcmp(char* buf1,char *buf2,unsigned count)
{
if (!count)
return(0);
while (--count && *buf1 == *buf2)
{
buf1++;
buf2++;
}
return(*buf1 - *buf2);
}边栏推荐
- [sequential logic circuit] - register
- Seminar 2022.07.22 -- Comparative learning
- Feature Selective Anchor-Free Module for Single-Shot Object Detection
- Harbor2.2 用户角色权限速查
- Riotboard development board series notes (IX) -- buildreoot porting matchbox
- Influxdb unauthorized access & CouchDB permission bypass
- 爬虫学习-概述
- php链路日志方案
- Single Gmv has increased 100 times. What is the "general rule" behind the rise of popular brands?
- System integration project management engineer (soft test intermediate) key knowledge, recitation version
猜你喜欢

【HiFlow】腾讯云HiFlow场景连接器实现校园信息管理智能化

Jenkins 详细部署

Stm32h750vbt6 drives programmable gain amplifier module pga113 -- Hal Library Based on cubemx

Riotboard development board series notes (IX) -- buildreoot porting matchbox

服务漏洞&FTP&RDP&SSH&rsync

Kali安装pip以及pip换源

Harbor2.2 用户角色权限速查

JS_ Realize the separation of multiple lines of text into an array according to the newline

Deep learning two or three things - review those classical convolutional neural networks
![[leetcode] 11. Container with the most water - go language solution](/img/42/3a1839dd768a5f02dc2acb5bd66438.png)
[leetcode] 11. Container with the most water - go language solution
随机推荐
从CIA看常见网络攻击(爆破,PE,流量攻击)
Induction, generalization, deduction
[leetcode] 444. Sequence reconstruction
[line test] Figure finding regular questions
爬虫学习-概述
24. Global event bus
C language file operation
File upload and download demo
Jackson parsing JSON detailed tutorial
[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled
MySQL语句
numpy.cumsum
Game three piece chess
中国三氯氢硅市场预测及战略研究报告(2022版)
There are two tables in Oracle, a and B. these two tables need to be associated with the third table C. how to update the field MJ1 in table a to the value MJ2 in table B
The goal you specified requires a project to execute but there is no POM in this directory
django.db.utils. OperationalError: (2002, “Can‘t connect to local MySQL server through socket ‘/var/r
Development system selection route
JS_实现多行文本根据换行分隔成数组
libsvm 使用参数的基础知识笔记(1)