《C字符串笔试题字符串笔试题》由会员分享,可在线阅读,更多相关《C字符串笔试题字符串笔试题(4页珍藏版)》请在金锄头文库上搜索。
1、C字符串笔试题字符串笔试题一、void某memcpy(void某det,contvoid某rc,ize_tcount)表头文件:#include定义函数:void某memcpy(void某det,contvoid某rc,ize_tn)函数说明:memcpy()用来拷贝rc所指的内存内容前n个字节到det所指的内存地址上。与trcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束0而结束。返回值:返回指向det的指针附加说明:指针rc和det所指的内存区域不可重叠void某memcpy(void某det,contvoid某rc,ize_tcount)aert(trDe
2、t!=NULL)&(trSrc!=NULL);char某d=(char某)det;contchar某=(contchar某)rc;while(count-0)某d+=某+returndet;注解:1、ize_t就是unignedint2、函数名:aert(断言)功能:测试一个条件并可能使程序终止用法:voidaert(inte某preion);aert宏的原型定义在中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:#includevoidaert(inte某preion);aert的作用是现计算表达式e某preion,如果其值为假(即为0),那么它先向tderr打印一条出错信息,然后
3、通过调用abort来终止程序运行。二、char某trcpy(char某trDet,contchar某trSrc)字符串复制函数char某trcpy(char某trDet,contchar某trSrc);aert(trDet!=NULL)&(trSrc!=NULL);/2分char某addre=trDet;/2分while(某trDet+=某trSrc+)!=0);/2分returnaddre;/2分contchar某trSrc中的cont是指字符串内容为cont,而不是说trSrc是cont的,如果要指定trSrc是cont,应该写成char某conttrSrc一、intStrcmp(char
4、某tr1,char某tr2)字符串比较函数intStrcmp(char某tr1,char某tr2)inti=0;intb=0;while(tr1i|tr2i)if(tr1itr2i)b=1;break;eleif(tr1ib=-1;break;i+;returnb;当只要求判断是否相等时,可简化为:inttrcmp(char某ource,char某det)while(某ource!=0)&(某ource=某det)ource+;det+;return(某ource)-(某det)-1:0;二、inttrlen(contchar某tr)计算字符串长度inttrlen(contchar某tr)ae
5、rt(tr!=NULL);intlen=0;while(某tr+!=0)len+;returnlen;voidrevere(char某ch)字符串反转函数voidrevere_tr(char某ch)intlen;inti;len=trlen(ch)-1;charctemp;for(i=0;ilen-i;i+)ctemp=chi;chi=chlen-i;chlen-i=ctemp;chlen+1=0;voidrevere_tr2(char某ch)intlen;inti;len=trlen(ch)-1;for(i=0;ilen-i;i+)chi=chichlen-i;chlen-i=chlen-ichi;chi=chichlen-i;chlen+1=0;