笔试题目2 字符串拷贝函数 char *strcpy(char *strDst,const char *strSrc)
来源:互联网 发布:手机音频后期制作软件 编辑:程序博客网 时间:2024/05/15 06:21
字符串函数原型为 char * strcpy(char *strDst,const char *strSrc)
char *strcpy(char *strDst,const char *strSrc)//使用const表明原字符串是不可变得,此外还用const来表示*strScr是输入参数
{
char *address=strDst;//保存目的串的首地址,因为接下来strDst指针是在移动的,最后不是指向字符串首地址的
if(strDst==NULL||strSrc==NULL)
return NULL;
if(strDst==strSrc)
return strDst;
while((*strDst++=*strSrc++)!='\0');/注意这里直接分号结束
return address;
}
注意要点:
1.传过来的原字符串内容在函数内不是不能改变的,因此应申明为const类型
2.要判断原指针或者目的指针为空的情况,思维要严谨,考虑异常情况
3.要考虑目的指针和原指针指向同一地址,这样就不用拷贝了,直接返回其中任意一个地址就行
4.函数返回值要是目的串的首地址,之所以返回类型还是字符指针,是要考虑拷贝函数的返回值可以进行其他操作。
5.在拷贝字符串时,有两种方式,先拷贝在判断字符为空 (上面代码是这种方式)要么先判断字符为空,然后拷贝再在最后补上‘\0’字符(它的数值是0,也就是ASCII为0)
char *strcpy(char *strDst,const char *strSrc)
{
char *address=strDst;
if(strDst==NULL||strSrc==NULL)
return NULL;
if(strDst==strSrc)
return strDst;
while((*strSrc!='\0')
{
*strDst++==*strSrc++;
}
*strDst='0';
return address;
}
- 笔试题目2 字符串拷贝函数 char *strcpy(char *strDst,const char *strSrc)
- 字符串拷贝函数 char *strcpy(char *strDst,const char *strSrc)
- 字符串拷贝函数 char *strcpy(char *strDest, const char *strSrc)
- 重写strcpy函数 char *StrCpy(char *strDest, const char *strSrc)
- 已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc)其中strDest 是目的字符串,strSrc 是源字符串。不调用C++/C
- 面试题:char * strcpy(char * strDest,const char * strSrc);
- 面试题:char * strcpy(char * strDest,const char * strSrc);
- 不用库函数,实现char* strcpy(char* strDes,const char* strSrc)函数
- 实现拷贝函数char* strcpy(char* dest,const char* src);
- C语言实现字符串拷贝函数char *strcpy(char *dest, const char *src)
- 字符串拷贝函数extern char *strcpy(char *dest,char *src);
- 笔试题目4 字符串连接函数 char *strcat(char *str1,const char *str2)
- 笔试题目3 字符串比较函数 int strcmp(const char* str1,const char *str2)
- char *strcpy(char *des, const char *src)函数
- 不采用strcpy库函数,完成字符串的拷贝。 strcpy(char *dest, char *src)
- error C2664:char *strcpy(char *,const char *)& 无法将参数 2 从CString"转换为const char *
- char数组和char*还有strcpy函数
- const char & char const
- 删除Oracle非活动Session的方法
- ASP.NET中的FILESTREAM对象总结
- 关于继承和虚函数的问题
- 输入框文本选择及焦点定位
- 初学C语言,请多多关照!共同学习进步
- 笔试题目2 字符串拷贝函数 char *strcpy(char *strDst,const char *strSrc)
- 编译器与解释器的不同
- tomcat 集群负载均衡
- 一封家书,道尽顶尖人才的思维境界(转载)
- 超链接 alt 提示效果
- Oracle恢复(六)------RMAN整库恢复
- 关于如何在Google Play Store 购买付费APP
- 批量导入数据触发器处理
- Partitioning and Formatting Second Hard Drive in Linux - (ext3)