【笔试】不调用C/C++字符串库函数,实现strcpy的功能

来源:互联网 发布:网络招聘信息平台 编辑:程序博客网 时间:2024/06/05 02:52
char* strcpy(char* strDest, char* strSrc){     if(strSrc==NULL || strDest==NULL)        throw "invalid expection(s)";           char* temp = strDest;     while((*strDest++ = *strSrc++) != '\0')          return temp;}
<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 2em; background-color: rgb(255, 255, 255);">       一. <strong>已知strcpy函数的</strong></span><strong><a target=_blank target="_blank" href="http://baike.baidu.com/view/228368.htm" style="font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 2em; text-decoration: none; color: rgb(19, 110, 194); background-color: rgb(255, 255, 255);">原型</a><span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 2em; background-color: rgb(255, 255, 255);">是:</span></strong>
char * strcpy(char * strDest,const char * strSrc);
⒈不调用库函数,实现strcpy函数。
⒉解释为什么要返回char *。
二. 解说
⒈strcpy的实现代码
char * strcpy(char * strDest,const char * strSrc)
{
if ((NULL==strDest) || (NULL==strSrc)) //[1]
throw "Invalid argument(s)"; //[2]
char * strDestCopy = strDest; //[3]
while ((*strDest++=*strSrc++)!='\0'); //[4]
return strDestCopy;
}
错误的做法:
[1]
(A)不检查指针的有效性,说明答题者不注重代码的健壮性。
(B)检查指针的有效性时使用((!strDest)||(!strSrc))或(!(strDest&&strSrc)),说明答题者对C语言中类型的隐式转换没有深刻认识。在本例中char *转换为bool即是类型隐式转换,这种功能虽然灵活,但更多的是导致出错概率增大和维护成本升高。所以C++专门增加了bool、true、false三个关键字以提供更安全的条件表达式。
(C)检查指针的有效性时使用((strDest==0)||(strSrc==0)),说明答题者不知道使用常量的好处。直接使用字面常量(如本例中的0)会减少程序的可维护性。0虽然简单,但程序中可能出现很多处对指针的检查,万一出现笔误,编译器不能发现,生成的程序内含逻辑错误,很难排除。而使用NULL代替0,如果出现拼写错误,编译器就会检查出来。
[2]
(A)return new string("Invalid argument(s)");,说明答题者根本不知道返回值的用途,并且他对内存泄漏也没有警惕心。从函数中返回函数体内分配的内存是十分危险的做法,他把释放内存的义务抛给不知情的调用者,绝大多数情况下,调用者不会释放内存,这导致内存泄漏。
(B)return 0;,说明答题者没有掌握异常机制。调用者有可能忘记检查返回值,调用者还可能无法检查返回值(见后面的链式表达式)。妄想让返回值肩负返回正确值和异常值的双重功能,其结果往往是两种功能都失效。应该以抛出异常来代替返回值,这样可以减轻调用者的负担、使错误不会被忽略、增强程序的可维护性。
[3]
(A)忘记保存原始的strDest值,说明答题者逻辑思维不严密。
[4]
(A)循环写成while (*strDestCopy++=*strSrc++);,同[1](B)。
(B)循环写成while (*strSrc!='\0') *strDest++=*strSrc++;,说明答题者对边界条件的检查不力。循环体结束后,strDest字符串的末尾没有正确地加上'\0'。
⒉返回strDest的原始值使函数能够支持链式表达式,增加了函数的“附加值”。同样功能的函数,如果能合理地提高的可用性,自然就更加理想。
链式表达式的形式如:
int iLength=strlen(strcpy(strA,strB));
又如:
char * strA=strcpy(new char[10],strB);
返回strSrc的原始值是错误的。其一,源字符串肯定是已知的,返回它没有意义。其二,不能支持形如第二例的表达式。其三,为了保护源字符串,形参用const限定strSrc所指的内容,把const char *作为char *返回,类型不符,编译报错。
在上面的语句中,循环语句
while ((*strDestCopy++=*strSrc++)!='\0');
较难理解,可以把这句理解为以下操作。
第一种:
while( 1 ){ char temp; temp = *strDestCopy = *strSrc; strDestCopy++; strSrc++; if( '\0' == temp )break;}
第二种:
while ( *strSrc != '\0' )
{
*strDestCopy = *strSrc;
strDestCopy++;
strSrc++;
}
*strDestCopy=*strSrc++;
也即:
while ( *strSrc != '\0' )
{
*strDestCopy ++= *strSrc++;
}
*strDestCopy=‘\0’;
使用实例:
//实例1:将一个字符串拷贝到一个足够长的字符数组中。本例中字符数组为a,长度为20。
//缺点:若数组长度不足以容纳整个字符串,则程序运行崩溃。
#include<iostream>
#include<stdlib.h>
using namespace std;
char * strcpy( char * strDest, const char * strSrc ){
char * strDestCopy = strDest;
if ((NULL==strDest)||(NULL==strSrc))throw "Invalid argument";
while ( (*strDest++=*strSrc++) != '\0' );
return strDestCopy;
}
void main( int argc, char * argv[] ){
char a[20], c[] = "i am teacher!";
try{
strcpy(a,c);
}
catch(char* strInfo) {
cout << strInfo << endl;
exit(-1);
}
cout << a << endl;
}
//实例2:预设两个字符指针,一个指向字符串,另一个为NULL,在程序运行过程中拷贝。
#include<iostream>
using namespace std;
char *strcpy(char *strDes, const char *strSrc); //函数声明
int main(){
const char *strSrc="helloworld";;
char *strDes=NULL;
strDes=strcpy(strDes,strSrc);
cout<<"strSrc="<<strSrc<<endl;
cout<<"strDes="<<strDes<<endl;
if(strDes!=NULL){
free(strDes);
strDes=NULL;
}
return 0;
}
char *strcpy(char *strDes, const char *strSrc){
assert(strSrc!=NULL); //若strSrc为NULL,则抛出异常。
strDes=(char *)malloc(strlen(strSrc)+1); //多一个空间用来存储字符串结束符'\0'
char *p=strDes;
while(*strSrc!='\0'){
*p++=*strSrc++;
}
*p='\0';
return strDes;
}
与strncpy的区别
第一种情况:
1
2
3
4
char* p="how are you ?";
char name[20]="ABCDEFGHIJKLMNOPQRS";
strcpy(name,p); //name改变为"how are you ? "====>正确!
strncpy(name,p, sizeof(name));//name改变为"how are you ?" =====>正确!后续的字符将置为NULL
第二种情况:
1
2
3
4
5
6
char* p="how are you ?";
char name[10];
strcpy(name,p); //目标串长度小于源串,错误!
name[sizeof(name)-1]='\0'; //和上一步组合,弥补结果,但是这种做法并不可取,因为上一步出错处理方式并不确定
strncpy(name,p,sizeof(name)); //源串长度大于指定拷贝的长度sizeof(name),注意在这种情况下不会自动在目标串后面加'\0'
name[sizeof(name)-1]='\0'; //和上一步组合,弥补结果

0 0