1.strstr
const char * strstr ( const char * str1, const char * str2 );
用途:看str1中是否有str2,若有则返回str1中相同的起始地址,若无则返回空指针
模拟实现1:
char* my_strstr(const char*str1,const char*str2)
{
char* p = str1;
char* s1 = NULL;
char* s2 = NULL;
while (*p)
{
s1 = p;
s2 = str2;
while (*s1 && *s2 && (*s1 == *s2))
{
s1++;
s2++;
}
if (*s2 == 0)
{
return p;
}
p++;
}
return NULL;
}
模拟实现2:
char* my_strstr(char* arr, char* arr2)
{
char* ret2 = arr2;
while(*(arr++)!=*(arr2)) {}
arr--;
if (*arr == '\0')
{
return NULL;
}
char* ret = arr;
while (*(arr++) == *(arr2++) && *(arr2)&&*(arr))
{}
if (*(arr2) == '\0')
{
return ret;
}
return my_strstr(ret+1,ret2);
}
2.strtok
char * strtok ( char * str, const char * delimiters );
第一个str为目标字符数组,第二个delimiters为需要分割的字符的数组
用途:从str中分割delimiters
模拟实现:
char* my_strtok(char*arr,const char *arr2)
{
static char*p;
char* tmp;
if (arr != NULL)
{
tmp = arr;
}
else
{
tmp = p+1;
}
char* start = tmp;
for (; *tmp; tmp++)
{
char* tmp2=arr2;
for (; *tmp2; tmp2++)
{
if (*tmp == *tmp2)
{
p = tmp;
*tmp = '\0';
return start;
}
}
}
if (*start != '\0')
{
p = tmp;
return start;
}
return NULL;
}
int main()
{
char arr[] = "hello*vantasy%you^are&good";
char arr2[] = "*%^&";
char temp[30] = {0};
strcpy(temp, arr);
char* temp2 = NULL;
for (temp2=my_strtok(temp, arr2); temp2 != NULL; )
{
printf("%s\n", temp2);
temp2 = my_strtok(NULL, arr2);
}
return 0;
}
3.strcmp(strncmp)
strcmp
int strcmp ( const char * str1, const char * str2 );
用途:依序比较str1和str2字符数组的大小
strncmp:
int strncmp ( const char * str1, const char * str2, size_t num )
用途:依序比较str1和str2的前num个字符数组的大小
模拟实现:
int my_strncmp(const char*str,const char*str2,size_t n)
{
int count = 1;
while (*str&&*str2&&*str==*str2&&count<n)
{
str++; str2++;
count++;
}
if (*str > *str2)
{
return 1;
}
else if (*str < *str2)
{
return -1;
}
else
{
return 0;
}
}
int main()
{
char arr[] = "abcde";
char arr2[] = "abcfe";
printf("%d", my_strncmp(arr, arr2, 4));
return 0;
}
4.strcat(strncat)
strcat
char * strcat ( char * destination, const char * source );
用途:在destination的字符串后面加上source的字符串
模拟实现1:
#include<assert.h>
char* my_strcat(char* arr, const char* arr2)
{
assert(arr && arr2);
char* ret = arr;
while (*arr++) {}
arr--;
while(*arr2)
{
*arr = *arr2;
arr++;
arr2++;
}
return ret;
}
int main()
{
char arr[20] = "hello";
char arr2[] = "Mr_vantasy";
printf("%s", my_strcat(arr, arr2));
return 0;
}
模拟实现2:
int main()
{
char arr[20] ="hello";
char arr2[] = "world";
sprintf(arr+strlen(arr), arr2);
printf(arr);
return 0;
}
strncat
char * strncat ( char * destination, const char * source, size_t num );
用途:在destination的字符串后面加上source的字符串的前n位
模拟实现:
#include<assert.h>
char* my_strncat(char* arr, const char* arr2,size_t num)
{
assert(arr && arr2);
int count=0;
char* ret = arr;
while (*arr++) {}
arr--;
while(*arr2&&count<num)
{
*arr = *arr2;
arr++;
arr2++;
count++;
}
return ret;
}
int main()
{
char arr[20] = "hello";
char arr2[] = "Mr_vantasy";
printf("%s", my_strncat(arr, arr2,3));
return 0;
}
5.strcpy(strncpy)
strcpy:
char * strcpy ( char * destination, const char * source );
用途:复制source的字符串到destination并覆盖
模拟实现:
#include<assert.h>
char* my_strncpy(char* str, char* str2)
{
assert(str && str2);
char* ret = str;
while (*str2)
{
*str = *str2;
str++;
str2++;
}
return ret;
}
int main()
{
char arr[20] = "hello__";
char arr2[] = "vantasy";
printf("%s", my_strncpy(arr, arr2));
return 0;
}
strncpy:
char * strncpy ( char * destination, const char * source, size_t num );
用途:复制source的n个字符到destination并覆盖
模拟实现:
#include<assert.h>
char* my_strncpy(char* str, char* str2,size_t num)
{
assert(str && str2);
char* ret = str;
int count = 0;
while (*str2&&count<num)
{
*str = *str2;
str++;
str2++;
count++;
}
return ret;
}
int main()
{
char arr[20] = "hello__";
char arr2[] = "vantasy";
printf("%s", my_strncpy(arr, arr2,3));
return 0;
}
后记:也许会有人说库里的函数直接用就行,为什么还要去模拟实现一下。但是模拟实现是帮我们更好的去理解这些库函数,更甚可以做到以后自己编写自己的库,学习前人的智慧并去模仿也是会让我们更上一层楼的(新人博主用词不准,或代码有误请多多指教…..φ(´U`●)╭(●`∀´●)╯╰(●’◡’●)╮ (●’◡’●)ノ )