当前位置:网站首页>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
代码有不足之处希望大家可以指出。
边栏推荐
猜你喜欢

BGP联邦实验

SDF refraction and reflection effect recording

动态规划-- 背包问题

Notes on the sixth day

What is Tianji 920 equivalent to a snapdragon? How much is Tianji 920 equivalent to a snapdragon? How about Tianji 920

581. 最短无序连续子数组

Detailed explanation of knapsack problem

Overlayfs source code parsing

Notes on the fifth day

200 lines of code, in-depth analysis of the principle and implementation of dynamic calculation diagram
随机推荐
ThreadLocal interview Kills 11 consecutive questions
Network security note 1 - Security of Internet Protocol
第九天笔记
多重背包!
mysql开启定时调度任务执行
达人评测酷睿i7 12850hx和i7 12700h选哪个
iQOO 10 Pro和vivo X80 Pro区别 哪个好详细参数配置对比
剑指offer19 正则表达式
STM32输出SPWM波,HAL库,cubeMX配置,滤波后输出1KHz正弦波
Day108.尚医通:医院模拟系统接口对接 - 医院|科室|排班 增删改分页条件查询
How to open the thought map pdf of postgraduate entrance examination in the small program of postgraduate entrance examination question bank
Changing the historical length of chart during LabVIEW operation
【模电复习——二极管】
rtx3070ti显卡什么水平 rtx3070ti显卡什么级别 rtx3070ti显卡怎么样
What level of rtx3070ti graphics card? What level of rtx3070ti graphics card? How about rtx3070ti graphics card
第十一天笔记
Rtx3080ti and rtx3080 gap 3080 and 3080ti parameter comparison
Kafka consumption reports an error coordinator unavailable Rediscovery will be attempt redisCovery
打家劫舍!
Pbootcms数据库转换教程(sqlite转mysql详细教程)