模拟实现string库函数
01 模拟实现strlen函数
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
//字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
//参数指向的字符串必须要以 '\0' 结束。
//注意函数的返回值为size_t,是无符号的( 易错 )
//学会strlen函数的模拟实现
size_t my_Strlen(const char *arr)
{
assert(arr != NULL);
size_t count=0;
int i = 0;
while (arr[i++] != '\0')
{
count++;
}
return count;
}
int main()
{
char a[10] = "wangzhen";
printf("%d", my_Strlen(a));
return 0;
}
02 模拟实现strcpy函数
代码:
#include<stdio.h>
#include<assert.h>
#include<string.h>
//源字符串必须以 '\0' 结束。
//会将源字符串中的 '\0' 拷贝到目标空间。
//目标空间必须足够大,以确保能存放源字符串。
//目标空间必须可变。
char* my_Strcpy(char * des, const char * sou)
{
char * address=des;
assert((des != NULL) && (sou != NULL));
while(*address++ = *sou++)
{ }
return des;
}
int main()
{
char a[] = "hello wangzhen";
char b[]="hello.c";
my_Strcpy(a, b);
printf("%s", a);
return 0;
}
03 模拟实现strncpy函数
代码:
#include<stdio.h>
#include<assert.h>
#include<string.h>
//拷贝num个字符从源字符串到目标空间。
//如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个
char * my_Strncpy(char * des, const char * sou, size_t num)
{
char * address = des;
assert((des != NULL) && (sou != NULL));
while (num--)
{
*address++ = *sou++;
}
return des;
}
int main()
{
char a[] = "hello wangzhen";
char b[] = "hello.c";
my_Strncpy(a, b, 6);
printf("%s", a);
return 0;
}
04 模拟实现strcat函数
代码:
#include<stdio.h>
#include<assert.h>
#include<string.h>
//源字符串必须以 '\0' 结束。
//目标空间必须有足够的大,能容纳下源字符串的内容。
//目标空间必须可修改。
//字符串自己给自己追加,如何?
char * my_Strcat(char * des, const char * sou)
{
char* address = des;
assert((des != NULL) && (sou != NULL));
while (*address!='\0')
{
address++;
}
while ((*address++ = *sou++) != '\0');
return des;
}
int main()
{
char a[20] = "hello wang";
char b[10] = "hello.c";
my_Strcat(a, b);
printf("%s", a);
return 0;
}
遇到的问题:第一次未给字符串规定大小,导致栈溢出,编译不通过。
05 模拟实现strncat函数
代码:
#include<stdio.h>
#include<assert.h>
#include<string.h>
char * my_Strncat(char * des, const char * sou, size_t num)
{
char* address = des;
assert((des != NULL) && (sou != NULL));
while (*address != '\0')
{
address++;
}
while (num--)
{
*address++ = *sou++;
}
*address = '\0';
return des;
}
int main()
{
char a[20] = "abcdefg";
char b[10] = "123456789";
my_Strncat(a, b, 6);
//strncat(a, b, 6);
printf("%s\n", a);
return 0;
}
06 模拟实现strcmp函数
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<string.h>
//第一个字符串大于第二个字符串,则返回大于0的数字
//第一个字符串等于第二个字符串,则返回0
//第一个字符串小于第二个字符串,则返回小于0的数字
//那么如何判断两个字符串?
int my_Strcmp(const char * str1, const char * str2)
{
assert((str1 != NULL) && (str2 != NULL));
int len1 = 0, len2 = 0;
while (*str1++)
len1++;
while (*str2++)
len2++;
return len1 - len2;
}
int main()
{
char a[] = "hello wang";
char b[] = "hello.c";
printf("%d", my_Strcmp(a, b));
return 0;
}
07 模拟实现strncmp函数
代码:
#include<stdio.h>
#include<assert.h>
#include<string.h>
int my_Strncmp(const char * str1, const char * str2, size_t num)
{
assert(str1 != NULL && str2 != NULL&&num>=0);
const char* pos1 = str1;
const char* pos2 = str2;
while (num!=0&&(*pos1==*pos2))
{
num--;
pos1++;
pos2++;
}
return *pos1-*pos2;
}
int main()
{
char a[10] = "abcdefghil";
char b[10] = "abcdefghil";
int ret = my_Strncmp(a, b, 5);
printf("%d\n", ret);
return 0;
}