当前位置:网站首页>c语言实现strcmp、strstr、strcat、strcpy
c语言实现strcmp、strstr、strcat、strcpy
2022-07-23 08:05:00 【久菜】
> strtok—切割字符串
> strcmp—比较字符串长度
> strstr—在字符串源串中寻找子串
> strcat—追加字符串
> strcpy—字符串拷贝
> strtok—切割字符串
下列是代码及需要注意的点
- strcmp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <assert.h>
//assert保证指针有意义,因为要解引用
//字符串比较函数--比较字符串长度
//模仿strcmp函数,三个返回值,>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
//
//查找子串函数-----------strstr
//功能:查找字符串中的子串
//原理:通过比较字符串的对应字符找出子串
//返回值:找到后返回子串地址,未找到返回NULL
//考虑两种情况:一次匹配(指针走一遍源串)和多次匹配(指针走多次源串,子串的指针也多次回到起点)
//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;//多次匹配时p的位置会一直向右走源串,直到\0
// s2 = str2;//多次匹配的时候str2要回到原来的位置
// while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)//bbbc(源串)
// { //bbc(子串)的情况
// s1++;
// s2++;
// }
// if (*s2 == '\0')//说明子串已经找完了,存在于源串中
// {
// return (char*)p;
// }
// p++;//--说明子串没走完,是由于第三个判断条件不符合跳出的循环,此时
// //-把p指向下一个字符,就可以检索出多次匹配的情况
// }
// return NULL;//源串已经走完了,还找不到子串
//}
//
//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("子串不存在\n");
// }
// else
// {
// printf("%s\n", ret);
// }
// return 0;
//}
- strcat
//------------字符串追加函数strcat
// 1.新追加的字符串要有\0
// 2.dest数组必须空间足够大,并且能够修改
// 3.先找到目标空间的结尾\0,再进行拷贝
//char* my_strcat(char* dest, const char* src)
//{
// assert(dest && src);
// char* ret = dest;
// //寻找
// while (*dest != '\0')
// {
// dest++;//循环结束的时候已经指向\0的下一个位置
// }
// //拷贝
// while (*dest++ = *src++)
// {
// ;
// }
// return ret;
//}
//int main()
//{
// char str[20] = "hello ";//标注空间大小
// my_strcat(str, "world");
// printf("%s\n", str);
// //当你想自己追加自己的时候,由于拷贝过程字符串的\0被覆盖,会造成死循环
// //my_strcat(str,str);----error
// return 0;
//}
- strcpy
//------------字符串拷贝函数my_strcpy
//源字符串必须以'\0'结束
//会将源字符串中的'\0'拷贝到目标空间
//目标空间必须足够大,以确保能存放源字符串
//目标空间必须可变
//
//char* my_strcpy(char* dest, const char* src)
//{
// assert(dest && src);
// char* ret = dest;
// while (*dest++ = *src++)//src的值为\0时赋值给dest,循环停止
// {
// ;
// }
// return ret;
//}
//int main()
//{
// char a[20] = { 0 }; 标注空间大小
// char b[] = "abcdef";
// my_strcpy(a,b);
// printf("%s", a);
// return 0;
//}
- strtok
//strtok切割字符串
//会将从分隔符开始的地方改成\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, " .-");//' '和'.'和'-'为分隔符
// }
// return 0;
//}
//打印结果:
//This
//a
//sample
//string
代码有不足之处希望大家可以指出。
边栏推荐
- 天玑920相当于骁龙什么 天玑920相当于骁龙多少 天玑920怎么样
- 第七天笔记
- Notes on the fourth day
- Notes on the seventh day
- JS数据类型判断方式总结
- 第八天笔记
- Process blocks and methods
- 强化學習——策略梯度理解點
- How about the performance of Intel Celeron 7305? What level is it equivalent to
- [baiqihang] Niuer education helps colleges and universities visit enterprises, expand posts and promote employment
猜你喜欢

天玑820相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑820怎么样

第十二天笔记

BGP联邦实验

Notes on the sixth day

How many processors is Tianji 820 equivalent to Xiaolong? How about Tianji 1100 equivalent to Xiaolong? How about Tianji 820

Detailed introduction of RIP

中等靶场

What level is rtx3090ti? What level is rtx3090ti graphics card? How about rtx3090ti graphics card

rtx3070ti显卡什么水平 rtx3070ti显卡什么级别 rtx3070ti显卡怎么样

Remember that a vulnhub target plane exercise successfully won the root permission
随机推荐
What's the difference between core i9 12950hx and i9 12900hx
第五天筆記
What level is the notebook core i5 1135g7 equivalent to? How about i5 1135g7 performance
shell跑的时候需要的需要了解命令
How many processors is Tianji 720 equivalent to Xiaolong? How about Tianji 720 equivalent to Xiaolong
MySQL enables scheduled task execution
小米12S Pro和小米12Pro天玑版区别 两者配置对比
Day 10 notes
Le shell a besoin de connaître les commandes
152. 乘积最大子数组
Network security note 1 - Security of Internet Protocol
pingbanceshi
Interface
将我理解的web3.0讲给你听
JS中不同的循环方式和注意事项总结
Canvas eraser function
Stream stream is used for classification display.
NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘
动态规划-- 背包问题
The difference between rtx3080ti and 3090. Which one is more cost-effective, rtx3080ti or 3090