当前位置:网站首页>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 .
边栏推荐
- ThreadLocal interview Kills 11 consecutive questions
- The shell needs to know the commands when running
- 初识并查集
- STM32输出SPWM波,HAL库,cubeMX配置,滤波后输出1KHz正弦波
- Chicken and egg, products and Strategies
- 【WinForm】关于截图识别数字并计算的桌面程序实现方案
- Renforcement de l'apprentissage - points de compréhension du gradient stratégique
- js日历样式饼图统计插件
- js纹理样式饼图插件
- 云呐-如何加强固定资产管理?怎么加强固定资产管理?
猜你喜欢
随机推荐
webstrom ERROR in [eslint] ESLint is not a constructor
js日历样式饼图统计插件
Surrounded Regions
shell跑的时候需要的需要了解命令
C语言实现memcpy、memmove
494. 目标和
Canvas eraser function
关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论
152. 乘积最大子数组
将我理解的web3.0讲给你听
Le shell a besoin de connaître les commandes
Aruba学习笔记05-配置架构- WLAN配置架构
pageHepler丢失原sql order by条件的坑
扁平样式反馈表单页面
4. 寻找两个正序数组的中位数
How many processors is Tianji 720 equivalent to Xiaolong? How about Tianji 720 equivalent to Xiaolong
优化华为云服务器采用Key登陆
Pacific Atlantic current problem
c语言实现strcmp、strstr、strcat、strcpy
JS texture style pie chart plug-in








