当前位置:网站首页>Detailed explanation of string + memory function (C language)
Detailed explanation of string + memory function (C language)
2022-07-28 03:52:00 【Brother Xiao who is not bald】
List of articles
Preface
C The processing of characters and strings in languages is very frequent , however C The language itself has no string type , Strings are usually placed in constant strings or character arrays . String constants apply to string functions that do not modify it .
String function
1.strlen
strlen It's a function of the length of a string , What I'm looking for is ’\0’ The number of characters before , There are three ways to achieve it
1.1 Counter mode
#include <stdio.h>
#include <assert.h>
size_t my_strlen(const char* str)
{
assert(str != NULL);// Prevent the pointer passed in from being null
int count = 0;
while (*str++)
{
count++;
}
return count;
}
1.2 recursively
#include <stdio.h>
#include <assert.h>
size_t my_strlen(const char* str)
{
assert(str != NULL);
if (*str)
{
return 1 + my_strlen(str+1);
}
else
{
return 0;
}
}
1.3 Pointer subtraction method
#include <stdio.h>
#include <assert.h>
size_t my_strlen(const char* str)
{
assert(str != NULL);
const char* p = str;
while (*str)
{
str++;
}
return str - p;
}
2.strcmp
strcmp Is a function that compares two strings
#include <assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2 && *str2)
{
str1++;
str2++;
}
return *str1 - *str2;
}
3.strcpy
strcpy Is a function that copies strings
#include <assert.h>
char* my_strcpy(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
4. strcat
strcat You can append a string after it
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest)
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
5.strncpy
char * strncpy ( char * destination, const char * source, size_t num );
- Copy num Characters from the source string to the target space .
- If the length of the source string is less than num, After copying the source string , Add... After the target 0, until num individual .
6. strncat
char * strncat ( char * destination, const char * source, size_t num );
- Before the source string num Characters are appended to the target , Plus the termination character .
- If the length of the source string is less than num, Then only the entire source string is appended to the target space .
7.strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
- Compare to a character that is different or a string ends or num Compare all the characters .
8.strstr
strstr Is a function to find a string
#include <stdio.h>
#include <assert.h>
char* my_strstr(const char* dest, const char* src)
{
assert(dest && src);
const char* str1, * str2;
while (*dest)
{
if (*dest == *src)
{
str1 = dest;
str2 = src;
while (str1 == str2 && *str2)
{
str1++;
str2++;
}
if (!*str2)
{
return dest;
}
}
dest++;
}
return NULL;
}
The above implementation method is obviously inefficient , Can use KMP The algorithm improves efficiency , I won't go over it here .
9.strtok
char * strtok ( char * str, const char * sep );
- sep The parameter is a string , Defines the set of characters used as separators
- The first parameter specifies a string , It contains 0 One or more by sep A mark separated by one or more separators in a string .
- strtok Function found str The next mark in , And use it \0 ending , Returns a pointer to the tag .( notes :strtok Function changes the string being manipulated , So it's using strtok The string cut by the function is usually a temporary copy and can be modified .)
- strtok The first argument of the function is not NULL , Function will find str The first mark in ,strtok Function will hold its position in the string .
- strtok The first argument to the function is NULL , The function will start at the same position in the string that is saved , Find next tag .
- If there are no more tags in the string , Then return to NULL The pointer .
10.strerror
char * strerror ( int errnum );
Return error code , The corresponding error message .
Example :
/* strerror example : error list */
#include <stdio.h>
#include <string.h>
#include <errno.h>// The header file that must be included
int main ()
{
FILE * pFile;
pFile = fopen ("unexist.ent","r");
if (pFile == NULL)
printf ("Error opening file unexist.ent: %s\n",strerror(errno));
//errno: Last error number
return 0;
}
Memory function
1.memcpy
- function memcpy from source The position of begins to be copied back num Bytes of data to destination Memory location for .
- This function is encountering ‘\0’ It doesn't stop .
- If source and destination There is any overlap , The results of replication are undefined .
void * memcpy ( void * dst, const void * src, size_t count)
{
void * ret = dst;
assert(dst);
assert(src);
//copy from lower addresses to higher addresses
while (count--)
{
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
return(ret);
}
2.memmove
- and memcpy The difference is that memmove The source and target memory blocks processed by the function can overlap .
- If the source space and the target space overlap , You have to use memmove Function processing .
void* my_memmove(void* dest,const void* src,size_t num)
{
void* ret = dest;
if (src > dest)
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
3.memcmp
int memcmp ( const void * ptr1,
const void * ptr2,
size_t num );
- Compare from ptr1 and ptr2 The pointer starts with num Bytes
- The return value is as follows

边栏推荐
- Fourier series
- [wrong question]
- deepstream 检测结果截图
- Leetcode58. 最后一个单词的长度
- Day08 redis的基础知识
- Prefix-Tuning: Optimizing Continuous Prompts for Generation
- Interface automation test, complete introduction
- Greed 122. The best time to buy and sell stocks II
- [force deduction] 1337. Row K with the weakest combat effectiveness in the matrix
- 动态规划——416. 分割等和子集
猜你喜欢

4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits

C language: find the number of 1 in binary stored in memory as an integer

Read Plato farm's eplato and the reason for its high premium

Appnium -- app automated test tool

Summary of static blog building tools

An article grasps the calculation and processing of date data in PostgreSQL

【原型与原型链】初识原型与原型链~

超好用的 PC 端长截图工具

Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions

动态规划——62. 不同路径
随机推荐
Mysql基础篇(创建、管理、增删改表)
In December, the PMP Exam adopted the new syllabus for the first time. How to learn?
Jumping game II in question 45 of C language power deduction. Ergodic jump
Read Plato farm's eplato and the reason for its high premium
Is there a bonus period for robot engineering
Summary of static blog building tools
贪心——122. 买卖股票的最佳时机 II
LeetCode 0140. 单词拆分 II
Implementation of online rental system based on SSM
Leetcode brush question: dynamic planning 09 (weight of the last stone II)
ES6 from entry to mastery 07: Deconstruction assignment
【OPENVX】对象基本使用之vx_matrix
【力扣】1337.矩阵中战斗力最弱的k行
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
Screenshot of deepstream detection results
Common weak network testing tools
MangoPapa 的实用小脚本(目录篇)
【P4】 查看库文件两个历史版本的区别
LeetCode_409_最长回文串
做自动化测试,你后悔了吗?