小肝出的字符串函数
以下代码所需要的头文件test.h
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
1.Strlen函数演示
int main()
{
char arr[] = "abc";
int len = strlen(arr);
printf("%d\n", len);
return 0;
}
Strlen函数的模拟实现
int my_strlen1(const char* str)
{
assert(str != NULL);
int count = 0;
while (*str != '\0')
{
str++;
count++;
}
return count;
}
int my_strlen2(const char* str)
{
assert(str != NULL);
int count = 0;
if (*str != '\0')
return my_strlen2(str + 1) + 1;
else
return 0;
}
int main()
{
char arr[] = "abc";
int len = my_strlen1(arr);
printf("%d\n", len);
return 0;
}
2.strcpy函数演示
int main()
{
char arr[20] = "##############";
strcpy(arr, "hello");
printf("%s\n", arr);
return 0;
}
strcpy函数的模拟实现
char* my_strcpy(char* dest, const char* str)
{
assert(str && dest);
char* ret = dest;
while (*dest++ = *str++)
{
;
}
return ret;
}
int main()
{
char arr[20] = "##############";
my_strcpy(arr, "hello");
printf("%s\n", arr);
return 0;
}
3.strcat函数的演示
int main()
{
char arr[20] = "hello";
strcat(arr, " world");
printf("%s\n", arr);
return 0;
}
strcat函数的模拟实现
char* my_strcat(char* dest, const char* str)
{
assert(str && dest);
char* ret = dest;
while(*dest)
{
dest++;
}
while (*dest++ = *str++)
{
;
}
return ret;
}
int main()
{
char arr[20] = "hello";
my_strcat(arr, " world");
printf("%s\n", arr);
return 0;
}
4.strcmp函数的演示
int main()
{
int ret1 = strcmp("abb", "ac");
printf("%d\n", ret1);
int ret2 = strcmp("ab", "ab");
printf("%d\n", ret2);
int ret3 = strcmp("ac", "abc");
printf("%d\n", ret3);
return 0;
}
strcmp函数的模拟实现
strcmp模拟
int my_strcmp(const char* p1, const char* p2)
{
assert(p1 && p2);
while (*p1 == *p2)
{
if (*p1 == '\0')
return 0;
*p1++;
*p2++;
}
if (*p1 > *p2)
return 1;
else
return -1;
}
int main()
{
char* p1 = "abcde";
char* p2 = "abcdf";
int ret = my_strcmp(p1, p2);
if (ret = -1)
printf("%s<%s\n", p1, p2);
else if (ret = 1)
printf("%s>%s\n", p1, p2);
else
printf("%s=%s\n", p1, p2);
return 0;
}
5.长度受限制的字符串函数模拟
strncpy函数
char* my_strncpy(char* dest, const char* str, size_t count)
{
char* ret = dest;
while (count && (*dest++ = *str++))
count--;
if (count) {
while (--count)
*dest++ = '\0';
}
return ret;
}
int main()
{
char arr1[] = "abcdefg";
char arr2[] = "qwer";
strncpy(arr1, arr2, 3);
printf("%s\n", arr1);
my_strncpy(arr1, arr2, 5);
printf("%s\n", arr1);
return 0;
}
strncmp函数
int my_strncmp(const char* dest, const char* src, int count)
{
if (!count)
return(0);
while (--count && *dest && *dest == *src)
dest++, src++;
return(*(unsigned char*)dest - *(unsigned char*)src);
}
int main()
{
char* p1 = "abcdefg";
char* p2 = "abcqwer";
int ret = my_strncmp(p1, p2, 3);
printf("%d\n", ret);
ret = my_strncmp(p1, p2, 4);
printf("%d\n", ret);
ret = my_strncmp(p1, p2, 5);
printf("%d\n", ret);
return 0;
}
strtok字符串分割函数的演示
int main()
{
char arr1[] = "fyh@zjut.edu";
char arr2[100] = { 0 };
char sep[] = "@.";
char* ret = NULL;
strcpy(arr2, arr1);
for (ret = strtok(arr2, sep); ret != NULL; ret = strtok(NULL, sep))
{
printf("%s\n", ret);
}
return 0;
}
strstr函数的模拟实现
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
char* s1;
char* s2;
char* cp = str1;
if (*str2 == '\0')
return str1;
while (*cp)
{
s1 = cp;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return cp;
}
cp++;
}
return NULL;
}
memcpy函数的模拟实现
void* my_memcpy(void* dest, void* src, size_t count)
{
void* ret = dest;
assert(dest && src);
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[20] = { 0 };
my_memcpy(arr2, arr1, 10 * sizeof(int));
int i;
for (i = 0; i < 20; i++)
{
printf("%d ", arr2[i]);
}
return 0;
memmove函数的模拟实现
void* my_memmove(void* dest, const void* src, size_t count)
{
void* ret = dest;
assert(src && dest);
if (dest < src)
{
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
while (count--)
{
*((char*)dest+count) = *((char*)src + count);
}
}
return ret;
}
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr, arr+2, 16);
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0a0eb04b04b0dad719977aa284293b51.jpeg#pic_center)