C语言,模拟实现strcpy、strlen函数
2016-10-16 16:05
411 查看
模拟实现strcpy、strlen函数
1、模拟实现strcpy
方法一:
#include<stdio.h> #include<string.h> int main() { char arr1[10] = {0}; char arr2[] = "abcdef"; strcpy(arr1, arr2);//arr1目标,arr1和arr2位置不能改变 printf("%s\n",arr1); return 0; }
方法二:
#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("%s\n",strcpy(arr,p)); return 0; }
2.模拟实现strlen
#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("%d\n",my_strlen("hello world"));
return 0;
}
相关文章推荐
- 【面试题】C语言:模拟实现strncpy,尝试比较strncpy与strcpy区别。
- [C语言]模拟实现strcpy/strncpy/strcat/strncat/strcmp/strncmp/strstr
- C语言-模拟实现strcpy
- C语言:向函数传递和返回字符串:Strlen函数、Strcpy、Strcat函数函数的实现
- 用c语言.模拟实现strcpy,strcat,strcat,memcpy,memmove
- 模拟实现strcpy和strlen函数
- c语言模拟实现strcpy
- C语言字符串函数的模拟实现(strlen、strcpy、strcat、strstr 、strcmp)
- 【面试题】C语言:模拟实现strncpy,尝试比较strncpy与strcpy区别。
- C语言:模拟实现strlen、strcpy、strcmp、strReverse、strcat函数
- 【C语言】模拟实现strcpy、strcat、strcmp函数
- c语言复习之模拟实现strlen函数、strcpy函数、strncpy函数、strcat函数、strncat函数
- C语言:模拟实现strcpy
- C语言:模拟实现strcpy
- C语言::模拟实现strlen函数
- C语言 模拟实现strcpy 与 strlen
- C语言:字符串函数(strcpy,memcpy等)模拟实现
- 模拟实现strcpy,strcmp,strstr,strncpy,strncmp,strcat,strlen函数
- C语言模拟实现strcpy、strncpy、strcmp、strcat、strstr
- C语言模拟实现strcpy、strlen、strcmp、strcat