当前位置:网站首页>String correlation function (II)
String correlation function (II)
2022-07-19 12:19:00 【Kkkkvvvvvxxx】
Catalog
1. Preface
Because the school final exam , It took 11 days to write this blog , The last blog introduced a part of string related functions and their implementation , This chapter introduces the remaining string functions and memory functions 
2. String function
2.1strtok

strtok Is a function of string segmentation , Through the set segmentation assignment, a string is divided into different strings .

Its usage rules are shown in the above figure , It looks awkward and cumbersome , Next, I will use examples to demonstrate directly :
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "[email protected]@qq.com.com";
const char *sep = "@.";
char str1[30];
// because strtok Will change the string being manipulated , So I make a temporary copy
strcpy(str1, str);
//1.kkk
char* ch = strtok(str1, sep);
printf("%s\n", ch);
//2.kkkkk
ch = strtok(NULL, sep);
printf("%s\n", ch);
//3.qq
ch = strtok(NULL, sep);
printf("%s\n", ch);
//4.com
ch = strtok(NULL, sep);
printf("%s\n", ch);
//5.com
ch = strtok(NULL, sep);
printf("%s\n", ch);
}
The debugging results are as follows :
The debugging results are shown in the figure , Every time a tag is found, it will be replaced by ‘\0’, And save the mark location , For the next time, the parameter is NULL Find other delimiters when .
improvement :
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "[email protected]@qq.com.com";
const char *sep = "@.";
char str1[30];
// because strtok Will change the string being manipulated , So I make a temporary copy
strcpy(str1, str);
char* ch = NULL;
for (
ch = strtok(str1, sep);
ch != NULL;
ch = strtok(NULL, sep)
)
{
printf("%s\n", ch);
}
}
2.2strerror

Is a return error code , A function that reports error information in a program .
#include<stdio.h>
#include<string.h>
#include<errno.h>
int main()
{
FILE* pf;
pf = fopen("test.txt", "r");
printf("%s", strerror(errno));
//errno Need a lead file #include<errno.h>
return 0;
}

The result is as follows. , You can use it !
3. Memory manipulation function
3.1memcmp


int main()
{
int arr1[] = {
1,2,3,4,5,7,7,7,10 };
int arr2[] = {
1,2,3,4,8,9,10,11,12 };
printf(" Integer array comparison :\n");
printf("%d\n", memcmp(arr1, arr2, 16));
printf("%d\n", memcmp(arr1, arr2, 28));
printf(" String comparison :\n");
char str1[] = "abcdefgh";
char str2[] = "abcdxxxx";
printf("%d\n", memcmp(str1, str2, 4));
printf("%d\n", memcmp(str1, str2, 6));
return 0;
}

3.2memcpy

memcpy Is a byte copy function , Same as strcpy Use consistent , The meaning of parameter is related to *memcmp It's about the same , I won't repeat it here , Next, directly implement memcpy function
void* my_memcpy(void* dest, const void* src, size_t num)
{
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
char str1[30] = {
0 };
char str2[] = "abcdefg";
my_memcpy(str1, str2, 6);
printf("%s\n", str1);
int arr1[] = {
1,2,3,4,5,6,7,8,9,10 };
int arr2[] = {
8,8,8,8,8,8,8,8,8,8 };
my_memcpy(arr1, arr2, 40);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}

What if we call the function like this ?
int main()
{
int arr1[] = {
1,2,3,4,5,6,7,8,9,10 };
my_memcpy(arr1+2, arr1, 20);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}


3.3memmove

memmove It is a library function used to deal with the problem of address overlap during assignment , Also with strcpy Have the same meaning , Used to copy data , However, it deals with data replication when space overlaps ; The specific test will not be demonstrated here , You can refer to memcpy Address overlaps for test cases !
void* my_memmove(void* dest, const void* src, size_t num)
{
void* ret = dest;
// front -> after
if (dest < src)
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
// after -> front
else
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr + 2, arr, 20);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
char str[] = "abcdefxxxxx";
my_memmove(str, str + 6, 5);
printf(str);
return 0;
}

3.4memset

memset It's an initialization function ;value Is the initial value ,num Yes bytes , This function actually initializes and assigns values to each byte .
int main()
{
char str1[10];
int arr1[10];
memset(str1, 0, 10);
memset(arr1, 0, 40);
return 0;
}

Ending
This blog is over here , That is, the string related functions are roughly listed , be it so *️*️
边栏推荐
猜你喜欢
随机推荐
HCIP (7)
Wi Fi sensing technology and practice based on channel state information
Configuring OSPF experiment in mGRE environment
详细分析一个ROS2 CMakeLists.txt文件
mysql学习笔记-约束
Familiar with nestjs (beginner)
01背包面试题系列(一)
使用原生js实现按钮的全选功能,简单清晰
Scrapy关键词 爬虫的简单实现(以新华网和人民网为例)
es安装ik分词器
C语言绘图示例-商标徽标
如何运行.sh脚本文件
Baidu document translation API
米哈游2023秋季招聘正式开始~提前批有机会免笔试!
字符串相关函数(二)
Research on downlink spectrum efficiency of 6G space earth integrated network high altitude platform base station
Use native JS to realize the function of selecting all buttons, which is simple and clear
MATLAB(4)函数及文件
Genesis与BlueRun Ventures展开深度交流
HCIP(4)









