C语言字符串库函数的实现
2017-08-14 00:00
323 查看
C语言字符串库函数的实现也是笔试题常考的题目,简单的实现:
//字符串长度
内存拷贝函数:
字符串比较函数:
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
相关文章推荐
- C语言字符串转成整数的函数实现
- C语言字符串操作函数实现
- C语言字符串库函数的实现(一)
- C语言中字符串系列的函数实现以及注意事项
- 用c语言实现 编写一个函数,将一个数字字符串转换成这个字符串对应的数字(包括正浮点数、负浮点数)
- C语言 编写一个函数reverse_string(char * string)(递归实现) 实现:将参数字符串中的字符反向排列
- C语言中字符串系列函数的实现以及注意事项
- 常用c语言字符串库函数(自行实现)
- C语言字符串库函数的实现
- 【c语言】字符串替换空格:请实现一个函数,把字符串中的每一个空格替换成“%20”
- 用C语言编写函数,实现strlen计算字符串长度的功能
- C语言实现的字符串截取函数
- C语言字符串相关库函数的实现strcpy、memcpy、strncpy、strcat、strncpy、memset、memcpy、strcpy
- C语言:编写reverse_string(char * string)(递归实现)函数,将参数字符串中的字符反向排列
- C语言中求字符串长度的函数的几种实现方法
- 【c语言】编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数
- C语言去除字符串首尾空格,trim()函数实现
- 用C语言(apue)实现 把时间戳转换为国标格式的字符串(2013-09-03 15:36:12)的函数
- C语言字符切割函数的实现——不需要自己指定分配多少个字符串分组