模拟实现strcpy、strlen函数
1、模拟实现strcpy
方法一:
复制代码
1
2
3
4
5
6
7
8
9
10#include<stdio.h> #include<string.h> int main() { char arr1[10] = {0}; char arr2[] = "abcdef"; strcpy(arr1, arr2);//arr1目标,arr1和arr2位置不能改变 printf("%sn",arr1); return 0; }
方法二:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include<stdio.h> #include<assert.h> char *my_strcpy(char *dest,const char *src)//dest= Destination,src=source { char *ret = dest; assert(dest);//assert确保你的程序按目标正常运行 assert(src); while(*dest++ = *src++) { ; } return ret; } int main() { char *p = "hello"; char arr[10]; printf("%sn",strcpy(arr,p)); return 0; }
2.模拟实现strlen
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<assert.h>
int my_strlen(const char *str)
{
int count = 0;
assert(str);
while(*str)
{
count++;
str++;
}
return count;
}
int main()
{
printf("%dn",my_strlen("hello world"));
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include<stdio.h> #include<assert.h> int my_strlen(const char *str) { int count = 0; assert(str); while(*str) { count++; str++; } return count; } int main() { printf("%dn",my_strlen("hello world")); return 0; }
最后
以上就是个性咖啡最近收集整理的关于C语言,模拟实现strcpy、strlen函数 模拟实现strcpy、strlen函数 的全部内容,更多相关C语言,模拟实现strcpy、strlen函数 内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复