当前位置:网站首页>Analyzing several common string library functions in C language
Analyzing several common string library functions in C language
2022-07-03 01:51:00 【Jarvis with stars and moon】
List of articles
Tips : The following is the main body of this article , The following cases can be used for reference
One 、 Introduce typical string library functions
1、 Function to find the length of string strlen
size_t strlen( const char* str);
The string has '\0' As an end sign ,strlen Function returns
In the string '\0' The number of characters that appear before ( It doesn't contain '\0').
Be careful : The string that the argument points to must be in '\0' end .
The return value of the function is size_t, It's a sign free .
Usage examples :
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = "ajhbduhr";
int i = strlen(arr);
printf("%d ", i);
}
2、 String copy function strcpy
char*strcpy(char*destination, const char* source);
notes : The source string must be in '\0' end .
In the source string '\0' Copy to target space .
The target space has to be large enough , To ensure that the source string can be stored .
The target space has to be variable .
Usage examples : Use strcpy after , take arr2 Will be copied to arr1 in
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[100] = {0};
char arr2[] = "abc";
strcpy(arr1,arr2);
printf("%s ", arr1);
}
3、 String append function strcat
char* strcat (char * destination, const char* source);
notes : The source string must be in '\0' end
The target space has to be large enough , To ensure that the source string can be stored .
Usage examples : Use strcat after ,arr1 You can add arr2 The content of 、
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[100] = "duhedu";
char arr2[] = "abc";
strcat(arr1,arr2);
printf("%s ", arr1);
}
4 String comparison function strcmp
int strcmp ( const char* str1, const char* str2);
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 ;
The first string is larger than the second string , Then return less than 0 The number of .
Usage examples : Positive number returned 1
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[100] = "duhedu";
char arr2[] = "abc";
int j = strcmp(arr1,arr2);
printf("%d", j);
}
5、 String lookup function strstr
char* strstr(const char* str1, const char* str2);
Its purpose is to find substrings in a string .
If it cannot be found, it returns a null pointer .
If it can be found, it will return the starting address of the substring of the string .
Usage examples : Return string arr1.
int main()
{
char arr1[] = "rnuvhvvr";
char arr2[] = "rnu";
char* j = strstr(arr1, arr2);
if (j == NULL)
printf(" Can't find ");
else
printf("%s", j);
return 0;
}
Two 、 Simulation Implementation of string library function
1、strlen Simulation Implementation of .( Counter method )
#include<stdio.h>
int my_strlen(char arr[])
{
int i = 0;
while (arr[i] != '\0')
{
i++;
}
return i;
}
int main()
{
char arr[] = "abcdefg";
int i = my_strlen(arr);
printf("%d\n", i);
return 0;
}
2、strcpy Simulation Implementation of .
char* my_strcpy(char* dest, const char* src)
{
char* ret = dest;
assert(dest && src);
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[] = "abcdef";
char arr2[10] = { 0 };
my_strcmp(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
3、strcat Simulation Implementation
char* my_strcat(char* dest, const char* src)// because dest Is to be modified , and src It can't be modified, so add const.
//char* my_strcat(char* strDestination, const char* strSource) The function finally returns strDestination
{
char* ret = dest;// Well preserved dest The address of the starting position of
assert(dest && src);//assert We can only judge whether it is NULL, It is impossible to judge whether it is a wild pointer .
while (*dest)// When dest No '\0'
{
dest++;
}
while (*dest++ = *src++)// Additional world To hello Of '\0' Location
{
;
}
return ret;//
}
int main()
{
char arr1[20] = "hello";
char arr2[] = "world";
my_strcat(arr1, arr2);// Additional
printf("%s\n", arr1);
return 0;
}
4、 strcmp Simulation Implementation
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
if (*str1 > *str2)
return 1;
else
return -1;
}
int main()
{
char arr1[] = "rnuvhvvr";
char arr2[] = "rnu";
char* j = strstr(arr1, arr2);
if (j == NULL)
printf(" Can't find ");
else
printf("%s", j);
return 0;
}
5、 strstr Simulation Implementation
char* my_strstr(char* str, char* substr)
{
assert(str && substr);
if (*substr == '\0') return str;
const char* s1 = str;
const char* s2 = substr;
char* cur = str;
while (*cur)
{
s1 = cur;
s2 = substr;
while (*s1 != '\0' && *s2 !='\0' && * s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
return cur;
cur++;
}
return NULL;
}
int main()
{
char arr1[] = "abcdefabcdef";
char arr2[] = "bcd";
char* ret = my_strstr(arr1, arr2);
if (ret == NULL)
printf(" Did not find \n");
else
printf("%s\n", ret);
return 0;
}
summary
Tips : Here is a summary of the article :
for example : That's what we're going to talk about today , This article only briefly introduces 5 The use of string functions and simple simulation implementation .
边栏推荐
- Network security - virus
- Reprint some Qt development experience written by great Xia 6.5
- [fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
- Network security - the simplest virus
- Learn the five skills you need to master in cloud computing application development
- Steps to obtain SSL certificate private key private key file
- 网络安全-病毒
- 【Camera专题】手把手撸一份驱动 到 点亮Camera
- 【数据挖掘】任务6:DBSCAN聚类
- Huakaiyun (Zhiyin) | virtual host: what is a virtual host
猜你喜欢
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
串口抓包/截断工具的安装及使用详解
简易分析fgui依赖关系工具
Rockchip3399 start auto load driver
小程序開發的部分功能
STM32 - introduction of external interrupts exti and NVIC
[camera topic] complete analysis of camera dtsi
High-Resolution Network (篇一):原理刨析
深度(穿透)选择器 ::v-deep/deep/及 > > >
Why is it not recommended to use BeanUtils in production?
随机推荐
Network security - Trojan horse
CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr
Network security - firewall
海量数据冷热分离方案与实践
Installation and use of serial port packet capturing / cutting tool
CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr
网络安全-NAT网络地址转换
Leetcode skimming questions_ Sum of two numbers II - enter an ordered array
Processing of tree structure data
转载收录6.5大侠写的部分Qt开发经验
Network security - dynamic routing protocol rip
网络安全-密码破解
Vant 实现简单的登录注册模块以及个人用户中心
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
[data mining] task 6: DBSCAN clustering
函数的定义和调用、this、严格模式、高阶函数、闭包、递归
View of MySQL
[camera topic] complete analysis of camera dtsi
SSL flood attack of DDoS attack
"Jetpack - livedata parsing"