您的位置:首页 > 编程语言 > C语言/C++

C语言字符串库函数的实现

2017-08-14 00:00 323 查看
C语言字符串库函数的实现也是笔试题常考的题目,简单的实现:


//字符串长度

intstrlen(constchar*str){assert(str!=NULL);intlen=0;while(*str++!='\0')++len;returnlen;}//字符串拷贝
char*strcpy(char*to,constchar*from){assert((to!=NULL)&&(from!=NULL));char*result=to;while((*to++=*from++)!='\0')NULL;returnresult;}//strncpy(),如果from指向的字符个数少于count,则用'\0'补齐
char*strncpy(char*to,constchar*from,size_tcount){assert((to!=NULL)&&(from!=NULL));char*result=to;while(count--){if(*from!='\0'){*to++=*from++;}else{*to++='\0';}}returnresult;}//memset():把指定内存区域的前count个字节设置成字符c
void*memset(void*buffer,intc,size_tcount){assert(buffer!=NULL);char*p=(char*)buffer;while(count--)*p++=(char)c;returnbuffer;}//查找字符串s中首次出现字符c的位置
char*strchr(char*str,intc){assert(str!=NULL);for(;*str!=(char)c;++str)if(*str=='\0')returnNULL;returnstr;}//字符串连接
char*strcat(char*strDes,constchar*strSrc){assert((strDes!=NULL)&&(strSrc!=NULL));char*address=strDes;while(*strDes!='\0')++strDes;while((*strDes++=*strSrc++)!='\0')NULL;returnaddress;}char*strncat(char*strDes,constchar*strSrc,unsignedintcount){assert((strDes!=NULL)&&(strSrc!=NULL));char*address=strDes;while(*strDes!='\0')++strDes;while(count--&&*strSrc!='\0')*strDes++=*strSrc++;*strDes='/0';returnaddress;}//查找字符串第一次出现的位置
char*strstr(constchar*strSrc,constchar*str){assert(strSrc!=NULL&&str!=NULL);constchar*s=strSrc;constchar*t=str;for(;*strSrc!='\0';++strSrc){for(s=strSrc,t=str;*t!='\0'&&*s==*t;++s,++t)NULL;if(*t=='\0')return(char*)strSrc;}returnNULL;}//将字符串拷贝到新的位置
char*strdup_(char*strSrc){if(strSrc!=NULL){char*start=strSrc;intlen=0;while(*strSrc++!='\0')len++;char*address=(char*)malloc(len+1);assert(address!=NULL);while((*address++=*start++)!='\0');returnaddress-(len+1);}returnNULL;}




内存拷贝函数:



//memcpy(),拷贝不重叠的内存块
void*memcpy(void*to,constvoid*from,size_tcount){assert((to!=NULL)&&(from!=NULL));void*result=to;char*pto=(char*)to;char*pfrom=(char*)from;assert(pto<pfrom||pto>pfrom+count-1);while(count--){*pto++=*pfrom++;}returnresult;}//memmove(),拷贝重叠或者是不重叠的内存块
void*memmove(void*to,constvoid*from,size_tcount){assert((to!=NULL)&&(from!=NULL));void*result=to;char*pto=(char*)to;char*pfrom=(char*)from;//to与from没有重叠
if(pto<pfrom||pto>pfrom+count-1){while(count--){*pto++=*pfrom++;}}//to与from有重叠,从后向前move
else{pto=pto+count-1;pfrom=pfrom+count-1;while(count--){*pto--=*pfrom--;}}returnresult;}




字符串比较函数:



//字符串比较
intstrcmp(constchar*s,constchar*t){assert(s!=NULL&&t!=NULL);while(*s&&*t&&*s==*t){++s;++t;}return(*s-*t);}//字符串比较(不区分大小写比较,大写字母会被映射为小写字母)
intstricmp(constchar*dst,constchar*src){assert(s!=NULL&&t!=NULL);intch1,ch2;while(*dst&&*src){if((ch1=(int)*dst)>='A'&&(ch1<='Z'))ch1+=0x20;if((ch2=(int)*src)>='A'&&(ch2<='Z'))ch2+=0x20;if(ch1==ch2){++dst;++src;}elsebreak;}return(ch1-ch2);}intstrncmp(constchar*s,constchar*t,unsignedintcount){assert((s!=NULL)&&(t!=NULL));while(*s&&*t&&*s==*t&&count--){++s;++t;}return(*s-*t);}




c标准库部分源代码



char*__cdeclstrcat(char*dst,constchar*src){char*cp=dst;while(*cp)cp++;/*findendofdst*/

while(*cp++=*src++);/*Copysrctoendofdst*/

return(dst);/*returndst*/}int__cdeclstrcmp(constchar*src,constchar*dst){intret=0;while(!(ret=*(unsignedchar*)src-*(unsignedchar*)dst)&&*dst)++src,++dst;if(ret<0)ret=-1;elseif(ret>0)ret=1;return(ret);}size_t__cdeclstrlen(constchar*str){constchar*eos=str;while(*eos++);return((int)(eos-str-1));}char*__cdeclstrncat(char*front,constchar*back,size_tcount){char*start=front;while(*front++);front--;while(count--)if(!(*front++=*back++))return(start);*front='\0';return(start);}int__cdeclstrncmp(constchar*first,constchar*last,size_tcount){if(!count)return(0);while(--count&&*first&&*first==*last){first++;last++;}return(*(unsignedchar*)first-*(unsignedchar*)last);}/*CopySRCtoDEST.*/
char*strcpy(char*dest,constchar*src){reg_charc;char*__unboundeds=(char*__unbounded)CHECK_BOUNDS_LOW(src);constptrdiff_toff=CHECK_BOUNDS_LOW(dest)-s-1;size_tn;do{c=*s++;s[off]=c;}while(c!='\0');n=s-src;(void)CHECK_BOUNDS_HIGH(src+n);(void)CHECK_BOUNDS_HIGH(dest+n);returndest;}char*__cdeclstrncpy(char*dest,constchar*source,size_tcount){char*start=dest;while(count&&(*dest++=*source++))/*copystring*/count--;if(count)/*padoutwithzeroes*/
while(--count)*dest++='\0';return(start);}


from:http://www.cnblogs.com/luxiaoxun/archive/2012/09/04/2670202.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐