您现在的位置是:首页 > Javascript
当前栏目
字符函数和字符串函数(下)——“C”
2023-04-18 14:15:21 时间
各位CSDN的uu们你们好呀,今天小雅兰的内容依旧是字符函数和字符串函数呀,这篇博客会讲一些内存相关的函数,下面,让我们进入字符函数和字符串函数的世界吧
字符串查找
strstr
strtok
错误信息报告
strerror
字符操作
内存操作函数
memcpy
memmove
memset
memcmp
strstr
char * strstr ( const char *str1, const char * str2);
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
#include<stdio.h> #include<string.h> #include<assert.h> int main() { char arr1[] = "abcdefbcdef"; char arr2[] = "bcd"; char* p = strstr(arr1, arr2); if (p == NULL) { printf("找不到 "); } else { printf("%s ", p); } return 0; }
模拟实现strstr
#include<stdio.h> #include<string.h> #include<assert.h> char* my_strstr(const char* str1, const char* str2) { char* s1 = NULL; char* s2 = NULL; char* cp = (char*)str1; while (*cp) { s1 = cp; s2 = (char*)str2; while (*s1 && *s2 && *s1 == *s2) { s1++; s2++; } if (*s2 == '