《字符串比较函数大全》

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

字符串比较函数2009-05-0212:06:17|分类:技术|字号订阅函数名:stpcpy功能:拷贝一个字符串到另一个用法:char*stpcpy(char*destin,char*source);程序例:#includestdio.h#includestring.hintmain(void){charstring[10];char*str1=abcdefghi;stpcpy(string,str1);printf(%s\n,string);return0;}函数名:strcat功能:字符串拼接函数用法:char*strcat(char*destin,char*source);程序例:#includestring.h#includestdio.hintmain(void){chardestination[25];char*blank=,*c=C++,*Borland=Borland;strcpy(destination,Borland);strcat(destination,blank);strcat(destination,c);printf(%s\n,destination);return0;}函数名:strchr功能:在一个串中查找给定字符的第一个匹配之处\用法:char*strchr(char*str,charc);程序例:#includestring.h#includestdio.hintmain(void){charstring[15];char*ptr,c='r';strcpy(string,Thisisastring);ptr=strchr(string,c);if(ptr)printf(Thecharacter%cisatposition:%d\n,c,ptr-string);elseprintf(Thecharacterwasnotfound\n);return0;}函数名:strcmp功能:串比较用法:intstrcmp(char*str1,char*str2);看Asic码,str1str2,返回值0;两串相等,返回0程序例:#includestring.h#includestdio.hintmain(void){char*buf1=aaa,*buf2=bbb,*buf3=ccc;intptr;ptr=strcmp(buf2,buf1);if(ptr0)printf(buffer2isgreaterthanbuffer1\n);elseprintf(buffer2islessthanbuffer1\n);ptr=strcmp(buf2,buf3);if(ptr0)printf(buffer2isgreaterthanbuffer3\n);elseprintf(buffer2islessthanbuffer3\n);return0;}函数名:strncmpi功能:将一个串中的一部分与另一个串比较,不管大小写用法:intstrncmpi(char*str1,char*str2,unsignedmaxlen);程序例:#includestring.h#includestdio.hintmain(void){char*buf1=BBB,*buf2=bbb;intptr;ptr=strcmpi(buf2,buf1);if(ptr0)printf(buffer2isgreaterthanbuffer1\n);if(ptr0)printf(buffer2islessthanbuffer1\n);if(ptr==0)printf(buffer2equalsbuffer1\n);return0;}函数名:strcpy功能:串拷贝用法:char*strcpy(char*str1,char*str2);程序例:#includestdio.h#includestring.hintmain(void){charstring[10];char*str1=abcdefghi;strcpy(string,str1);printf(%s\n,string);return0;}函数名:strcspn功能:在串中查找第一个给定字符集内容的段用法:intstrcspn(char*str1,char*str2);程序例:#includestdio.h#includestring.h#includealloc.hintmain(void){char*string1=1234567890;char*string2=747DC8;intlength;length=strcspn(string1,string2);printf(Characterwherestringsintersectisatposition%d\n,length);return0;}函数名:strdup功能:将串拷贝到新建的位置处用法:char*strdup(char*str);程序例:#includestdio.h#includestring.h#includealloc.hintmain(void){char*dup_str,*string=abcde;dup_str=strdup(string);printf(%s\n,dup_str);free(dup_str);return0;}函数名:stricmp功能:以大小写不敏感方式比较两个串用法:intstricmp(char*str1,char*str2);程序例:#includestring.h#includestdio.hintmain(void){char*buf1=BBB,*buf2=bbb;intptr;ptr=stricmp(buf2,buf1);if(ptr0)printf(buffer2isgreaterthanbuffer1\n);if(ptr0)printf(buffer2islessthanbuffer1\n);if(ptr==0)printf(buffer2equalsbuffer1\n);return0;}函数名:strerror功能:返回指向错误信息字符串的指针用法:char*strerror(interrnum);程序例:#includestdio.h#includeerrno.hintmain(void){char*buffer;buffer=strerror(errno);printf(Error:%s\n,buffer);return0;}函数名:strcmpi功能:将一个串与另一个比较,不管大小写用法:intstrcmpi(char*str1,char*str2);程序例:#includestring.h#includestdio.hintmain(void){char*buf1=BBB,*buf2=bbb;intptr;ptr=strcmpi(buf2,buf1);if(ptr0)printf(buffer2isgreaterthanbuffer1\n);if(ptr0)printf(buffer2islessthanbuffer1\n);if(ptr==0)printf(buffer2equalsbuffer1\n);return0;}函数名:strncmp功能:串比较用法:intstrncmp(char*str1,char*str2,intmaxlen);程序例:#includestring.h#includestdio.hintmain(void){char*buf1=aaabbb,*buf2=bbbccc,*buf3=ccc;intptr;ptr=strncmp(buf2,buf1,3);if(ptr0)printf(buffer2isgreaterthanbuffer1\n);elseprintf(buffer2islessthanbuffer1\n);ptr=strncmp(buf2,buf3,3);if(ptr0)printf(buffer2isgreaterthanbuffer3\n);elseprintf(buffer2islessthanbuffer3\n);return(0);}函数名:strncmpi功能:把串中的一部分与另一串中的一部分比较,不管大小写用法:intstrncmpi(char*str1,char*str2);程序例:#includestring.h#includestdio.hintmain(void){char*buf1=BBBccc,*buf2=bbbccc;intptr;ptr=strncmpi(buf2,buf1,3);if(ptr0)printf(buffer2isgreaterthanbuffer1\n);if(ptr0)printf(buffer2islessthanbuffer1\n);if(ptr==0)printf(buffer2equalsbuffer1\n);return0;}函数名:strncpy功能:串拷贝用法:char*strncpy(char*destin,char*source,intmaxlen);程序例:#includestdio.h#includestring.hintmain(void){charstring[10];char*str1=abcdefghi;strncpy(string,str1,3);string[3]='\0';printf(%s\n,string);return0;}函数名:strnicmp功能:不注重大小写地比较两个串用法:intstrnicmp(char*str1,char*str2,unsignedmaxlen);程序例:#includestring.h#includestdio.hintmain(void){char*buf1=BBBccc,*buf2=bbbccc;intptr;ptr=strnicmp(buf2,buf1,3);if(ptr0)printf(buffer2isgreaterthanbuffer1\n);if(ptr0)printf(buffer2islessthanbuffer1\n);if(ptr==0)printf(buffer2equalsbuffer1\n);return0;}函数名:strnset功能:将一个串中的所有字符都设为指定字符用法:char*strnset(char*str,charch,unsignedn);程序例:#includestdio.h#includestring.hintmain(void){char*string=abcdefghijklmnopqrstuvwxyz;charletter='x';printf(stringbeforestrnset:%s\n,string);strnset(string,letter,13);printf(stringafterstrnset:%s\n,string);return0;}函数名:strpbrk功能:在串中查找给定字符集中的字符用法:char*strpbrk(char*str1,char*str2);程序例:#includestdio.h#includestring.hintmain(void){char*string1=abcdefghijklmnopqrstuvwxyz;char*string2=onm;char*ptr;ptr=strpbrk(string1,string2);if(ptr)printf(strpbrkfoundfirstcharacter:%c\n,*ptr);elseprintf(strpbrkdidn'tfindcharacterinset\n);return0;}函数名:strrchr功能:在串中查找指定字符的最后一个出现用法:char*strrchr(

1 / 24
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功