当前位置:网站首页>C language implements StrCmp, strstr, strcat, strcpy
C language implements StrCmp, strstr, strcat, strcpy
2022-07-23 14:31:00 【Jiucai】
> strtok— Cut string
> strcmp— Compare string length
> strstr— Look for substrings in the string source string
> strcat— Append string
> strcpy— String copy
> strtok— Cut string
The following are the codes and points needing attention
- strcmp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <assert.h>
//assert Make sure the pointer is meaningful , Because you need to dereference
// String comparison function -- Compare string length
// imitation strcmp function , Three return values ,>0,<0,=0;
//int my_strcmp(const char* str1, const char* str2)
//{
// assert(str1 && str2);
// while ( *str1==*str2 )
// {
// if (*str1 == '\0')
// {
// return 0;
// }
// str1++;
// str2++;
// }
// return (*str1 - *str2);
//}
//int main()
//{
// char a[] = "abcdef";
// char b[] = "abcde";
// printf("%d",my_strcmp(a, b));
// return 0;
//}
- strstr
//
// Find substring function -----------strstr
// function : Find the substring in the string
// principle : Find the substring by comparing the corresponding characters of the string
// Return value : Return the substring address after finding , No return found NULL
// Consider two situations : One match ( The pointer goes through the source string ) And multiple matches ( The pointer walks multiple times in the source string , The pointer of the substring also returns to the starting point many times )
//char* my_strstr(const char* str1, const char* str2)
//{
// assert(str1 && str2);
// const char* s1 = str1;
// const char* s2 = str2;
// const char* p = str1;
//
// while (*p)
// {
// s1 = p;// When matching multiple times p The position of the source string will go all the way to the right , until \0
// s2 = str2;// When matching many times str2 To return to the original position
// while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)//bbbc( The source string )
// { //bbc( Substring ) The situation of
// s1++;
// s2++;
// }
// if (*s2 == '\0')// It means that the substring has been found , Exists in the source string
// {
// return (char*)p;
// }
// p++;//-- It means that the substring is not finished , It is because the third judgment condition does not meet the jump cycle , here
// //- hold p Point to the next character , You can retrieve multiple matches
// }
// return NULL;// The source string has gone , No substring found yet
//}
//
//int main()
//{
// char email[] = "aaaabc";
// char substr[] = "abc";
// char* ret = my_strstr(email, substr);
// //char arr1[] = "abcdef";
// //char arr2[] = "def";
// //char* ret = my_strstr(arr1, arr2);
//
// if (ret == NULL)
// {
// printf(" Substring does not exist \n");
// }
// else
// {
// printf("%s\n", ret);
// }
// return 0;
//}
- strcat
//------------ String append function strcat
// 1. The newly appended string should have \0
// 2.dest The array must be large enough , And can be modified
// 3. Find the end of the target space first \0, Copy again
//char* my_strcat(char* dest, const char* src)
//{
// assert(dest && src);
// char* ret = dest;
// // seek
// while (*dest != '\0')
// {
// dest++;// When the loop ends, it points to \0 Next position of
// }
// // Copy
// while (*dest++ = *src++)
// {
// ;
// }
// return ret;
//}
//int main()
//{
// char str[20] = "hello ";// Mark the space size
// my_strcat(str, "world");
// printf("%s\n", str);
// // When you want to add yourself , Due to the \0 Be overwritten , It's going to create a dead cycle
// //my_strcat(str,str);----error
// return 0;
//}
- strcpy
//------------ String copy function my_strcpy
// 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
//
//char* my_strcpy(char* dest, const char* src)
//{
// assert(dest && src);
// char* ret = dest;
// while (*dest++ = *src++)//src The value of is \0 Assign a value to dest, The cycle stops
// {
// ;
// }
// return ret;
//}
//int main()
//{
// char a[20] = { 0 }; Mark the space size
// char b[] = "abcdef";
// my_strcpy(a,b);
// printf("%s", a);
// return 0;
//}
- strtok
//strtok Cut string
// The place starting from the separator will be changed to \0
//int main()
//{
// char str[] = "- This, a sample string.";
// char* pch;
// printf("Splitting string \"%s\" into tokens:\n", str);
// pch = strtok(str, " ,.-");
// while (pch != NULL)
// {
// printf("%s\n", pch);
// pch = strtok(NULL, " .-");//' ' and '.' and '-' Separator
// }
// return 0;
//}
// Print the results :
//This
//a
//sample
//string
I hope you can point out the shortcomings of the code .
边栏推荐
- C语言实现memcpy、memmove
- Pagehepler lost the pit of the original SQL order by condition
- 云呐-如何加强固定资产管理?怎么加强固定资产管理?
- OKRK3399開發板預留I2C4掛載EEPROM
- Tensor、Numpy、PIL格式转换以及图像显示
- JS中不同的循环方式和注意事项总结
- 数据库连接池 & DBUtils
- Cool code rain dynamic background registration page
- Stream stream is used for classification display.
- JS software unloading prompt expression changes with the mouse JS special effect
猜你喜欢
随机推荐
将我理解的web3.0讲给你听
云呐|怎样管理固定资产?如何进行固定资产管理?
Day 5 experiment
太平洋大西洋水流问题
Chapter 2 basic query and sorting
Is it risky and safe to open an account for stock speculation?
Uiscrollview (uicollectionview) prohibits horizontal and vertical sliding at the same time
Canvas eraser function
[download attached] several scripts commonly used in penetration testing that are worth collecting
完全背包!
Tensor、Numpy、PIL格式转换以及图像显示
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
VK36N5D抗电源干扰/手机干扰 5键5通道触摸检测芯片防呆功能触摸区域积水仍可操作
数据库连接池 & DBUtils
LZ77文件压缩
Chicken and egg, products and Strategies
Interface
Find the maximum area of the island -- depth first search (staining method)
152. 乘积最大子数组
wacom固件更新错误123,数位板驱动更新不了



![寻找峰值[抽象二分练习]](/img/99/122e79784f0f07120680d2cbcf89da.png)




